You must Sign In to post a response.
  • Category: JQuery

    Set Selected item in dropdown using jquery

    0 down vote favorite

    I m having two dropdownlist A and B.Dropdown A will loaded at the time of page load .Dropdown B will load based on dropdown A's selected item and value and I m loading Dropdown B using Json .

    Now the problem is I m having a table and i m getting edit values from that html table and I need to refill these values to corresponding dropdowns.

    First I set the selected item for Dropdown A by

    $("#ctl00_MainContent_A option:contains("'Test' ")").prop('selected', 'selected');

    Its working fine , and I load dropdown B by using the A's Selected item text by using Json.

    After load B,the new options are loaded into Dropdown B and I m trying to set selected text for dropdown B by same above

    $("#ctl00_MainContent_B option:contains("'Test B' ")").prop('selected', 'selected');

    now the operation not working . I checked ,when i use each function for Dropdown B's option

    $("#ctl00_MainContent_B > option").each(function() {
    alert(this.text + ' ' + this.value);

    Its just give the only one item text and value in alert, not giving the all newly loaded options text and values in alert.

    Please give me any solution ..

    Thanks in advance
  • #756343
    Using the value:

    $('[name=options]').val( 3 );

    To reset it use:

    $('[name=options]').val( '' );

    Using the text:

    $('[name=options] option').filter(function() {
    return ($(this).text() == 'Test');
    }).prop('selected', true);

  • #756346
    Since you are not able to set the correct value in second DropDownList there are two reasons.

    1. Either the value you are setting is not available in the DropDownList.
    2. Your second dropdownlist is not populated properly.

    Asheej T K

  • #757498
    Hi Ramesh,

    as you said its problem of the time dilay. i think your code is trying to set the dropdown value before its loaded from json, then its not able to set the value, try to identify the load first then set the value then you can archive your target.

    if you want more help then share your code snippet then we can suggest you in better way.


  • Sign In to post your comments