Search code examples
javascriptjqueryjquery-on

Problems Using .on() with Dynamically Added Elements


I read that .live() has been deprecated, and that I should use .on instead. But .on doesn't seem to be working for elements added to the DOM.

My script adds a table with any number of text boxes (input type="text"), and I want to run some script when the content in any of those text boxes changes.

Here's part of my code. vendorsPopUp references the div that contains my table.

$('input', vendorsPopUp).on('change', function (e) {
    alert($(this).attr('class'));
});

But this code does not run when the content in a text box changes.


Solution

  • It should be like this

    $(vendorsPopUp).on('change', 'input', function (e) {
       alert($(this).attr('class'));
    });