0

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?

3
  • It looks like you're trying to run before you can walk – you should be comfortable with the basics of jQuery before writing a plugin. Here's a good place to start: docs.jquery.com/Tutorials. Commented Jul 16, 2011 at 11:18
  • I'm working with JQuery for the past two years. Just got mixed up with the documentation for writing a plugin where binding the events was mentioned. I fixed it before I got the answer here :). Got confused with .bind(). Commented Jul 16, 2011 at 11:22
  • Ok. Perhaps you were focusing too much on the documentation, as after two years you should instinctively know exactly what the jQuery object you instantiate represents and how to navigate it – the fact that you're writing a plugin is not relevant to your question. Good luck with the plugin, though. Commented Jul 16, 2011 at 12:58

1 Answer 1

3

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
});
Sign up to request clarification or add additional context in comments.

1 Comment

Okie it is like a part of long table. I've edited the question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.