PDA

View Full Version : jQuery Registering an element into the DOM


Fumigator
03-22-2009, 05:18 AM
I'm using the html() modification function to create an <img> tag with a certain class name. The image appears fine, but I have a jQuery event $(".classname").click() that will not register on this new image I've created. It works on all images with that class name otherwise; just the images that I dynamically create using html() don't seem to get properly added to the DOM in a place where they are then recognized by other jQuery actions.

Is there a way to get this to work?

Event code on image click is:


$(".editbtn").click(function() {
alert("got clicked");
});


Code that creates the image is:


$("#tnbrdisp").html(textString + '<img id="edittrknbr" class="editbtn" src="images/edit.png" alt="edit number" title="edit number" />');

Eldarrion
03-22-2009, 09:51 PM
Try the following:


$(".editbtn").live('click', function() {
alert("got clicked");
});


Should do the trick. And of course, if you want to remove the event at some point...


$(".editbtn").die('click');


Enjoy.