2

I have the following issue :

1/ Using Django, I am generating a form containing a set of countries.

The form is as follows :

country=forms.CharField(max_length=20
                     , widget=forms.Select(choices=countries)
                     , label=_('Country'))

The generated html is as follows

<div class="form-group">
   <label class="col-lg-1" for="id_country"> … </label>
   <div class="col-lg-1">
      <select id="id_country" class="chzn-done" name="country" style="display: none;"> … </select>
      <div id="id_country_chzn" class="chzn-container chzn-container-single" style="width: 291px;" title=""> 
   </div>
</div>
</div>

2- I'd like to update the select dynamically using javascript

The code to update the select is provided below. The option is correctly changed. However, the "id_country_chzn" is not changed. The consequence is that although the option is changed, the value displayed for the end user is not changed. Is there a special trigger required ?

The javascript code to update the option in the select is provided below. Could someone help me if I am missing anything ?

town.val('14').trigger('change');

Thanks a lot for your help

1 Answer 1

1

When you manually change select value, you need to trigger the changes as well:

$("#id_country").trigger("chosen:updated");

http://harvesthq.github.io/chosen/#change-update-events

Sign up to request clarification or add additional context in comments.

2 Comments

Hi, I have tried but it didn't change anything on the value displayed for the user. I have tried with $("#id_country").trigger("chosen:updated"); and with $("#id_country_chzn").trigger("chosen:updated");
Dear all, ok I have now dowloaded the latest version of chosen libraries and it works ! Thanks so much mariodev

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.