Alex Rivera | Logout

Chosen select an option by default with jQuery

Asked 2012-02-06T13:42:28.820
28

I have one select box with options and I am using jQuery "Chosen" on a Zend View. This plugin is working fine.

Javascript:
$('#select_type').chosen();

My problem is I need to select one of the options when the page loads depending on what the value of type_id is. I tried this:

document.getElementById('select_type').value = '<?=$this->type_id?>';

This changes its value but the option is not selected in the JQuery chosen.

Any ideas?


EDIT: The option successfully changes by doing what I have mentioned above but it does not update/change the selected element in jQuery "chosen" plugin. My question is about updating the selected value for jQuery Chosen not the HTML SELECT (which I have done successfully). Here is a link to jQuery Chosen plugin

Edit
Report

1 Answer

7

You can just set the correct elements with the attribute selected like so:

<select name="teams[]" data-placeholder="Chose an option:" 
        class="chzn-select" multiple tabindex="6">
    <option value=""></option>
    <option selected="selected">Dallas Cowboys</option>
    <option selected="selected">New York Giants</option>
    <option>Philadelphia Eagles</option>
    <option>Washington Redskins</option>
</select>

Chosen will take care to initialize itself and show the two pre-selected choices.

answered 2012-11-26T00:04:03.340

Your Answer