CodingForums.com

CodingForums.com (http://www.codingforums.com/index.php)
-   JavaScript frameworks (http://www.codingforums.com/forumdisplay.php?f=62)
-   -   jQuery Registering an element into the DOM (http://www.codingforums.com/showthread.php?t=161882)

Fumigator 03-22-2009 04:18 AM

Registering an element into the DOM
 
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:

Code:

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

Code that creates the image is:

Code:

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

Eldarrion 03-22-2009 08:51 PM

Try the following:

Code:

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

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

Code:

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


All times are GMT +1. The time now is 09:44 PM.

Powered by vBulletin®
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.