0

I have created a new html element, and inserted it onto a node. However it doesn't have any listeners registered on the class name 'edit-expense-category' because of this. My code for registering this class name on page load is as follows:

$(".edit-expense-category").click(function () { 
  // function related stuff
});

What I'd like to know is once I have another element created dynamically using the class name above, how do I register this element with a listener as well?

floriank
  • 25,546
  • 9
  • 42
  • 66
user1658296
  • 1,398
  • 2
  • 18
  • 46

1 Answers1

2

I was going to suggest you using jQuery's .live, but it turns out it is deprecated. However, the newer equivalent is .on. Use it like this:

$( document ).on( events, selector, data, handler );  

// In your specific case it'll look like:
$( document ).on( "click", ".edit-expense-category", { }, function () {
    // function related stuff
});  

More about this here (don't forget to look into the newer equivalent of .on)

leopik
  • 2,323
  • 2
  • 17
  • 29
  • Thanks, I had to use .live - the framework that I've been given to work with is so ancient that it Js version doesn't support .on :( – user1658296 Apr 16 '15 at 07:56