2
\$\begingroup\$

I have a form that reads a JSON file for default wrong answers to be applied to a 'summary div' at the end of the form.

I wondered if I could make these two functions one somehow?

It is a bootstrap modal, that I am using data-attributes to get the right answers which populate the text in the modal body, then when I click 'save changes' button I am repeating myself but wondered if I could make this more efficient.

$('#ncModal').on('show.bs.modal', function(e) {
    var btn = $(e.relatedTarget);
    var reciepient = btn.data('whatever');

    if (reciepient === "@mdo") {
        console.log(reciepient);
        $.getJSON('./NCText/solar-nc.json', function (data) {
            //console.log(data);
            //console.log(data[0]);
            var output = '<ul>';
            $.each(data[0], function(key, val) {
                output += '<li>' + val + '</li>';
            });

            output += '</ul>';
            $('#modalbody').html(output); // replace all existing content
        });
    }

    // get Action Improvement text clicked to add
    // to summary box.
    $('.modal-body').on('click', function () {
        var text = $(event.target).text();
        console.log(text);
        $('#summary').append(text).append('\n');
    });

    // Add Action Improvement textarea to summary
    $('#saveModalNc').on('click', function () {
        var txt = $('#nc_textarea').val();
        console.log(txt);
        $('#summary').append(txt);
    });
});
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.