user3093453
user3093453

Reputation: 697

Chosen plugin change event not triggering

I'm using Chosen jQuery plugin and noticed that the change event is working only when the page loads, NOT every time the input field is being change.

How can I make it work every time the user changes the value of the input field?

Here is my code:

 $("#day").chosen().change({
     console.log('working');
  });

Am I missing something?

Upvotes: 30

Views: 59664

Answers (6)

Cyril Jacquart
Cyril Jacquart

Reputation: 2762

chosen multiple

When chosen multiple is set, $(this).val(); returns an array. The following code will return the last id [un]selected:

$('#my_chosen_list').chosen().change(function(_, object){
    console.log(!object.selected ? object.selected : object.deselected);
    ...
});

tip: undefined is undefined (...), but !undefined is true, so !!undefined is false

Upvotes: 1

Muddassir Irahad
Muddassir Irahad

Reputation: 41

    //Asp.net dropdown listbox
  <asp:ListBox ID="CompanySelect" runat="server" AppendDataBoundItems="true" 
  AutoPostBack="true"   
  data-placeholder="Select Companies" class="chosen-select"  SelectionMode="Multiple" 
  EnableViewState="true" Height="39px" Width="99%" >
  <asp:ListItem Value="0">All</asp:ListItem>
  </asp:ListBox>

    //This code help me for hiting chozen change event.

     $('#chosen').val('').change()
        {
           alert("working");
        }

    //if you want to get select value you need to do this.

     $('#chosen').val('').change()
        {
            alert($(".chosen-select").val());
        }

Upvotes: 0

ace313
ace313

Reputation: 247

Try this:

$(document).ready(function(){
    $("selector").chosen().change(function(){
        var id = $(this).val();
    });
})

Upvotes: 23

AkshayKumar
AkshayKumar

Reputation: 125

Updating Chosen Dynamically

If you need to update the options in your select field and want Chosen to pick up the changes, you'll need to trigger the "chosen:updated" event on the field. Chosen will re-build itself based on the updated content.

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

Upvotes: 7

Soundar
Soundar

Reputation: 2589

To fire the standard change event, use like:

  $('#day').on('change', function(e) {
    // triggers when whole value changed
    console.log("value changed");
  });

To fire the event on each key press,

  $('#day').on('keyup', function(e) {
    // triggers when each key pressed
    console.log("key pressed");
  });

To know about the default events of chosen, refer here.

Upvotes: 28

Milind Anantwar
Milind Anantwar

Reputation: 82241

Then Write it in keyup event of input:

$('inputselector').keyup(function() {
  $("#day").chosen().change({
   console.log('working');
  });
});​

and on dom ready:

$('inputselector').trigger('keyup');

Upvotes: 0

Related Questions