Praveen
Praveen

Reputation: 56539

Update options in select dynamically

I have constructed a dynamic select using jQuery like below:

Based on YesOrNo, I have to show/hide options in Source and Type

Yes/No          Source               Type
---------------------------------------------
Yes             Yes1 and Yes2        Yes
No              No1 and No2          No1 to 6

HTML:

<select id="YesOrNo" name='YesOrNo'>
    <option value=''>Select Yes/No</option>
    <option value='1'>Yes</option>
    <option value='2'>No</option>
</select>
<select id='Source' name='Source'>
    <option value=''>Select Source</option>
    <option data-aob='Yes' value='1'>Yes1</option>
    <option data-aob='Yes' value='2'>Yes2</option>
    <option data-aob='No' value='3'>No1</option>
    <option data-aob='No' value='4'>No2</option>
</select>
<select id="Type" name='Type'>
    <option value=''>Select Type</option>
    <option data-aob='No' value='1'>No1</option>
    <option data-aob='No' value='2'>No2</option>
    <option data-aob='No' value='3'>No3</option>
    <option data-aob='No' value='4'>No4</option>
    <option data-aob='Yes' value='5'>Yes</option>
    <option data-aob='No' value='6'>No5</option>
    <option data-aob='No' value='7'>No6</option>
</select>

JQuery:

$('#YesOrNo').on('change', function () {
    if (this.value === '1') {
        $('#Source option[data-aob=Yes]').show();
        $('#Source option[data-aob=No]').hide();
        $('#Type option[data-aob=Yes]').show();
        $('#Type option[data-aob=No]').hide();
        $("#Source option:selected").prop("selected", false);
        $("#Type option:selected").prop("selected", false);
    } else if (this.value === "2") {
        $('#Source option[data-aob=Yes]').hide();
        $('#Source option[data-aob=No]').show();
        $('#Type option[data-aob=Yes]').hide();
        $('#Type option[data-aob=No]').show();
        $("#Source option:selected").prop("selected", false);
        $("#Type option:selected").prop("selected", false);
    } else {
        $('#Source option').show();
        $('#Type option').show();
        $("#Source option:selected").prop("selected", false);
        $("#Type option:selected").prop("selected", false);
    }
});

Here is my JSFiddle

This is working perfect as I expected. Since the values are obtained from database, I have a problem while displaying the selected option in YesOrNo.

<select id="YesOrNo" name='YesOrNo'>
            <option value=''>Select Yes/No</option>
            <option selected value='1'>Yes</option>
            <option value='2'>No</option>
          </select>

Here the option "Yes" is selected by default, but here http://jsfiddle.net/ubVfa/1/ still showing the four options and Type showing all 7 options.

Yes/No                            Source               Type
--------------------------------------------------------------------
Yes(SELECTED by default)          All 4 options        All 7 Options

How to tune my code to update the options in select dynamically.

Any help is appreciated.

Upvotes: 1

Views: 2556

Answers (4)

H.D.
H.D.

Reputation: 4454

You can call $("...").change() after the page is loaded:

$(function(){
    $('#YesOrNo').change();
});

See my fiddle:

I also hid the options when nothing is selected, calling $("...").hide() in the "onchange" event handler.

The $(func) with func as an anonymous function is a syntatic suggar for $(document).ready(func), which calls that given function after loading.

The "change" call is just a trigger call, like $("...").trigger("change").

Upvotes: 4

LoneWOLFs
LoneWOLFs

Reputation: 2306

If your code logic allows this trigger a change event once.

//Triggering a change event on document ready
$(document).ready(function(){    
    $('#YesOrNo').trigger('change');
});

JS FIDDLE

Upvotes: 0

Dipesh Parmar
Dipesh Parmar

Reputation: 27382

You can simply do this by triggering change event on load.

just add below code inside document.ready

$('#YesOrNo').trigger('change');

and for test it add selected="selected" for No option and run the code.

See Demo

Upvotes: 1

Arun P Johny
Arun P Johny

Reputation: 388436

You can use trigger to manually invoke the handler on page load.... still your solution will not work in IE... see further changes below

var sourceHtml = $('#Source').html();
var typeHtml = $('#Type').html();
$('#YesOrNo').on('change', function () {
    var sources = $(sourceHtml);
    var types = $(typeHtml);
    var aob = $(this).find('option:selected').data('aob');

    if(aob){
        sources = sources.filter('[data-aob="' + aob + '"]');
        types = types.filter('[data-aob="' + aob + '"]');
    }
    $('#Source').empty().append(sources)
    $('#Type').empty().append(types)
}).triggerHandler('change');

Demo: Fiddle

Upvotes: 2

Related Questions