seedg
seedg

Reputation: 21935

Manually selecting a multiple drop down option if value is matched in JQuery

I need to manually select a dropdown option if a value is matched. Here is the code:

if($("#hiddenMyField").val() != "") {
            $("#dropdown").each(function(i){
                 $('option', this).each(function() {
                     if($(this).html() == $("#hiddenMyField").val()) {
                         // code to select the option
                     } else {
                         alert('not matched');
                     }

                 });

            });
        }

How can I select the current option located in the drop down if that if condition is met?

Thanks

Upvotes: 1

Views: 1345

Answers (3)

seedg
seedg

Reputation: 21935

All right I managed to find a workaround for this.

Since the options were being translated to divs with checkboxes with the ui.dropdownchecklist.js I first loaded the multiple dropdown with its normal view, then selected the necessary items with the this.selected = true and then I loaded the ui.dropdownchecklist.js function so the items will be translated back to divs with checkboxes. The user doesn't even see the actual multiple checkboxes so this worked out well for me. When they are translated to checkboxes, the selected items are kept and are also ticked when they are transferred.

Upvotes: 1

jAndy
jAndy

Reputation: 235972

$(this).attr('selected', true);

should do the magic.

Upvotes: 1

RoToRa
RoToRa

Reputation: 38390

Options have a selected property:

this.selected = true;

Upvotes: 1

Related Questions