InGeek
InGeek

Reputation: 2682

Reselect the chosen option with 'chosen' plugin. JQuery

I am using the Harvest chosen plugin with Jquery. Everything works fine and when I select one of the options it becomes selected, with some styles applied to it: I'am using (Allow Deselect on Single Select): link.

The problem is when I refresh the page I want that selected value to be re rendered.

I tried to understand Chosen properties an methods, such as: Chosen.result_activate() or Chosen.result_select(), but I'am not sure how to use them.

here is my html:

        <div id="container">
    <div class="side-by-side clearfix">
           <div class="side-by-side clearfix">
        <select id ="combooptions" data-placeholder="Search drafts" style="width:200px" class="chzn-select-deselect" tabindex="7">
          <option value=""></option>
 </select><span style="margin-left:5px;cursor:pointer;color:blue;text-decoration:underline;" id="draftInsert"> Insert</span>
      </div>
    </div>
   </div> 

EDIT:

actually my sample is not in jQuery(document).ready() but in separate function, which is reloaded all the time.And is actually not refresh page but a updatepanel refresh. sorry for misunderstanding. My page is not reloading the document.ready I guess.

and here is my js:

    var saveseleted='';
    jQuery(document).ready(function ($) {

    for (var i in combonews) {
            jQuery("#combooptions").append("<option value='" + combonews[i][0] + "'>" + combonews[i][1] + "</option>");
               }

        jQuery(".chzn-select-deselect").chosen({ allow_single_deselect: true });

jQuery('#combooptions').change(function () {
        saveseleted= jQuery("#combooptions :selected").val();
    });
    });

and then I after page refresh I need the 'saveselected' option value to be re-chosen automatically.

Please help me,

UPDATE:

jQuery("#combooptions option").eq(saveseleted).attr('selected', 'selected');

Doesn't work

Upvotes: 0

Views: 4082

Answers (1)

luckystars
luckystars

Reputation: 1754

I am guessing this could be the answer:

 jQuery("#combooptions").val(saveseleted);  //update the value first

 jQuery("#combooptions").trigger("liszt:updated");   //trigger this event

This trigger event will force the plugin to get updated.

Upvotes: 3

Related Questions