SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Evangelist
    Join Date
    Nov 2009
    Posts
    471
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Placement of jQuery event handler

    I have a situation where a portion of the page is built based on a javascript action when the page is loading. In the code that is loaded there is an anchor tag. I am putting a click event listener on the a tag with jQuery.

    Originally I was just doing that listener somewhere in the .js files that load initially. It wasn't working. I moved the listener declaration to within the function that actually calls the replaceWith function and put it after the replaceWith command and now it works.

    This means that now it will be declared whenever the function containing the replaceWith function is called and the content changes but it works. Is that the only way this works?

    I suspect that when I was doing it originally, the anchor tag didn't exist. So I dynamically added the anchor tag and the event listener didn't recognize it. Is that what is happening?

  2. #2
    SitePoint Evangelist
    Join Date
    Nov 2009
    Posts
    471
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I think I figured it out. If I use .live('click', function... it should handle dynamically created tags and event. Is that the correct approach. It seems to work.


Bookmarks

Posting Permissions

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