Padyster
Padyster

Reputation: 1003

Jquery - Form submit gives a dropdown selected value, How to set associated html using jquery?

I have a form, which submits a retains the dropdown value previously selected, initially, before submitting the form, on selecting any option in dropdown used to display or hide some htmls elements, using jquery, now the problem is how to use jquery on finding the selected value after form submit to control the other html element like it does before submit.

The code below is when I manually change the value in dropdown:

$(document).ready(function() {
    $('#representative').change(function(){
        var rep = $('#representative').val();
        if(rep == 'Golfer'){
            golfer();
        } else if (rep == 'Bussiness') {
            bussiness();
        } else {
            def();
        }
    });
});

I am looking for the way If after submitting the form when I get any preselected value in the dropdown, I must get either the function bussiness(); to executed or function golfer(); should be executed.

Upvotes: 0

Views: 1520

Answers (1)

Ohgodwhy
Ohgodwhy

Reputation: 50767

All you need to do is move your variable check outside of the scope, and get the selected value.

var selectedRep = $('#representative').val();
if(selectedRep == 'Golfer'){
    golfer();
}else if(selectedRep == 'Bussiness'){
    bussiness();
}else{
    def();
}

Simply place that inside of your $(document).ready({ // code here }); function, and it will evaluate when the page loads.

Upvotes: 2

Related Questions