Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jquery .change() function not working with dynamically populated SELECT list

I have a select field that is dynamically populated with an ajax call that simply returns all the HTML select options. Here is the part of the PHP that just echo's the select tags and dynamically fills out each option/value.

echo "<select name='player1' class='affector'>";
echo "<option value='' selected>--".sizeof($start)." PLAYERS LOADED--</option>";

foreach ($start as $value) {
    echo "<option value='".$value."'>".$value."</option>";
 }  
  echo "</select>";
}  

After this list is populated, I'm trying to call a change event, so that whenever the default option is changed in the SELECT list or in a text field with the same class, it disables a radio button set in another part of the form. (You can see the initial question I asked to get this part of the functionality working here)

$('.affector').change(function(){
       $(this).parents('tr').find('input:radio').attr('disabled', false);
});

For some reason, even though I give the select field the proper class name (affector), when I select different options in the field, the other parts of the form do not disable. The static text field with the same class works fine. I'm stumped.

Any ideas?

like image 921
tresstylez Avatar asked Jan 09 '10 00:01

tresstylez


3 Answers

Just commented on your last question...Here's what I said:

Use jQuery bind

function addSelectChange() {
   $('select').bind('change', function() {
       // yada yada
   });
} 

Call addSelectChange in your ajax success function. It should do the trick. Take a look at jQuery live event too (for when you want to set events for all current and future DOM elements in a later project or something). Unfortunately the change event and a few others aren't yet supported with live. Bind is the next best thing

like image 179
munch Avatar answered Nov 12 '22 09:11

munch


A more up to date answer..

Since the elements are dynamically populated, you are looking for event delegation.

Don't use .live()/.bind()/.delegate(), though. You should use .on().

According to the jQuery API docs:

As of jQuery 1.7, the .on() method is the preferred method for attaching event handlers to a document. For earlier versions, the .bind() method is used for attaching an event handler directly to elements. Handlers are attached to the currently selected elements in the jQuery object, so those elements must exist at the point the call to .bind() occurs. For more flexible event binding, see the discussion of event delegation in .on().

Therefore you would use something like this:

$(document).on('change', 'select', function (e) {
    /* ... */
});
like image 26
Josh Crozier Avatar answered Nov 12 '22 10:11

Josh Crozier


An example using .live()

$('#my_form_id select[name^="my_select_name"]').live('change', (function () {
    console.log( $("option:selected", this).val());
    })
 );
like image 5
Tapefreak Avatar answered Nov 12 '22 09:11

Tapefreak