Link to home
Start Free TrialLog in
Avatar of Keisha Smith
Keisha Smith

asked on

Modal Form Submit Button Not Working When Clicked

I created a modal dropdown form with four dropdown lists, with the fourth dropdown containing a link to a document to display once user clicks the submit button. All four dropdowns work fine, however when clicking the submit button the document does not display. The form simply continues to display, log indicates 'Cannot read property '0' of undefined'.. Any help is appreciated.

var selectedOptions = {};

        $('#link1').on('change', function () {
            var a = $(this).val();
            selectedOptions['1'] = a;
            selectedOptions['2'] = a;
            selectedOptions['3'] = a;
            if (a !== '') {
                for (var i = 0; i < dataSecondSelect[a].length; i++) {
                    $('#link2').append($("<option></option>")
                            .attr("value", dataSecondSelect[a][i])
                            .text(dataSecondSelect[a][i]));
                }
            }
        });

        $('#link2').on('change', function () {
            var a = $(this).val();
            selectedOptions['1'] = a;
            selectedOptions['2'] = a;
            selectedOptions['3'] = a;
            if (a !== '') {
                for (var i = 0; i < dataThirdSelect[a].length; i++) {
                    $('#link3').append($("<option></option>")
                            .attr("value", dataThirdSelect[a][i])
                            .text(dataThirdSelect[a][i]));
                }
            }
        });
        $('#link3').on('change', function () {
            var a = $(this).val();
            selectedOptions['1'] = a;
            selectedOptions['2'] = a;
            selectedOptions['3'] = a;
            if (a !== '') {
                for (var i = 0; i < dataFourthSelect[a].length; i++) {
                    $('#link4').append($("<option></option>")
                            .attr("value", dataFourthSelect[a][i].link)
                            .text(dataFourthSelect[a][i].form));
                }
            }
        });

    $('#clickButton').on('click', function () {
        var error = false;
        $(".error").remove();
        $(".validation-error").removeClass('validation-error');
        $('#myModal select').each(function () {
            // validate first
            if ($(this).val() === "") {
                var _message = "Please select an option";
                $(this).addClass('validation-error');
                $(this).after($('<div class="error"></div>').text(_message));
                error=true;
            }
        });

        if (error) { return; }
        // form is now validated so get the link
        var _index = $("#link4").val();
        var _form = dataFourthSelect[_index][0].link;
        resetForm($(this)[0]);
        $('#myModal').modal('hide');
        openDoc(_form);

    });

    function resetForm(e) {
        $(".error").remove();
        $(".validation-error").removeClass('validation-error');
        e.form.reset();
    }

</script>

Open in new window

Avatar of Ron Malmstead
Ron Malmstead
Flag of United States of America image

At what line number is the error throwing?

This error is very common actually, but you need to know what code line number is causing it first and foremost.
ASKER CERTIFIED SOLUTION
Avatar of David S.
David S.
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Keisha Smith
Keisha Smith

ASKER

Removed the fourthdata select link.