Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts

    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" />');

  • #2
    Regular Coder
    Join Date
    Feb 2009
    Location
    Wheeling, IL
    Posts
    358
    Thanks
    5
    Thanked 62 Times in 60 Posts
    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.
    The way to success is to assume that there are no impossible things. After all, if you think something is impossible, you will not even try to do it.

    How to ask smart questions?

  • Users who have thanked Eldarrion for this post:

    Fumigator (04-03-2009)


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •