NLV
NLV

Reputation: 21641

JQuery Plugin - Binding an event to a dynamic element

I'm building my first JQuery plugin and learning the standards for the plugins. I'm creating dynamic elements in my plugin like this -

var control = $("<table><tr><td><div>Test Control</div></td><td><img src='' /></td></tr></table>")
//Add the control to the document

Now how can I register the click event of the div inside the table?

Upvotes: 0

Views: 358

Answers (1)

pixelfreak
pixelfreak

Reputation: 17834

Like so (I added a class and replaced it with anchor, just so it's more specific and semantic):

var control = $('<table><tr><td><a class="test" href="#">Test Control</a></td><td><img src='' /></td></tr></table>');
// Add control to document
control.find('a.test').click(function(e)
{
   // Do whatever...
   e.preventDefault(); // Prevent default behavior of the anchor
});

Upvotes: 3

Related Questions