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") {
        $.getJSON('./NCText/solar-nc.json', function (data) {
            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();

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

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.