0

My change function works great for select controls that are built into the page initially, but when I dynamically add a select control, the change function won't fire.

<select name="SUBJ" class="SUBJ" id="SUBJ_-9">
   <option value="1">First item</option>
   <option value="2">Second item</option>
</select>

$j('.SUBJ').change(function () {

    alert("foo");

}

I'm wondering after I dynamically add the control:

$j("tbody > tr:last").prev().before(strAppend);

do I need to call any function to associate the event watcher/handler with the control?

Any other ideas? Tks!

SlimJim
  • 367
  • 3
  • 5
  • 12

3 Answers3

2

you need to bind the event.try like this:

$j('body').on('change','.SUBJ',function() {

    alert("foo");

});
Suchit kumar
  • 11,809
  • 3
  • 22
  • 44
1

Because you are generating HTML dynamically with Jquery, so try event delegation as shown :-

$j('body').on('change','.SUBJ',function() {
    alert("foo");
});

OR

$j(document).on('change','.SUBJ',function() {
    alert("foo");
});

Reference.

Kartikeya Khosla
  • 18,743
  • 8
  • 43
  • 69
1

Your syntax is a little off, you're missing a ')' at the end. It should look like

$j('.SUBJ').change(function () {

     alert("foo");

});
Alex J
  • 1,029
  • 6
  • 8