pedalpete
pedalpete

Reputation: 21536

trigger a 'change' action when a dropdown is 'selected' not when the dropdown list values are changed

I've got a few dropdown lists which are created dynamically.

I've done a decent job of keeping my code DRY, but I have a small problem.

I instruct jQuery to trigger an event when a select list 'changes', and seeing as the list are built dynamically, they change when a response is retrieved from the server, which jQuery is considering a change. But what I actually want is for the action to be triggered only when an option is 'selected' from the dropdown list.

$('select').change(function(){
       var idx = $(this).index();
       var query = $(this).val();
    $.ajax({
        url: 'getNext',
        data: {query:query},
        dataType: 'json',
        success: function(data){
                    var optionsList =''; 
                      for (var j=0; j'+data[j]+'';
                        }
                     $('select:eq('+idx+1+')').html(optionsList);
                         }
     });
});

I've tried using

$('select:selected').change()
but that doesn't trigger anything. Any suggestions on how to only trigger change when an option is selected, not when an option or select list is created?

Upvotes: 2

Views: 2447

Answers (2)

Vivek
Vivek

Reputation: 11028

Try trigger

$('select:selected').trigger('change'); 

Upvotes: 0

David Thomas
David Thomas

Reputation: 253318

Have you tried:

$('select').click(
    function(){
        $(this).trigger('change');
    });

Upvotes: 0

Related Questions