SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Guru downtroden's Avatar
    Join Date
    Dec 2004
    Location
    illinois
    Posts
    972
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    .live() working on new elements but not existing elements

    So i've got a form that adds an element onto the page. This is working. When I try to remove said elements, that works. But the same 'delete' button doesn't work on elements not generated by javascript.

    Code JavaScript:
    	function destroyQuickTask() {
    		$.post($(this).attr("href"), null, null, "script");
    		return false;
    	}
    	$('a.destroy_quick_task').live('click', destroyQuickTask);

    Is there something wrong with the way I'm using .live()? It basically is saying 'pay attention to all a.destroy_quick_task elements that exist now AND in the future, right? Seems like it's fine with future elements and can't deal with existing ones.
    your brain reacts in the same way whether you are
    looking at something or thinking about it...

  2. #2
    SitePoint Enthusiast
    Join Date
    Sep 2006
    Posts
    26
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yes, .live() works on new elements only.
    If you need attach function to existing elements, use .bind() instead.
    ArticleQueen: First Article Rewriter with Lexical Processor!

  3. #3
    SitePoint Guru downtroden's Avatar
    Join Date
    Dec 2004
    Location
    illinois
    Posts
    972
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    so is there a function that will work on existing elements AND new elements? Kinda thought live served that purpose, it was explained to me as so… 

    "Find all elements on the website with class name "destroy_quick_task", and add the onclick event to fire the destroyQuickTask function AND also add this same onclick event to all elements with class name "destroy_quick_task" that do not exist but might be created in the future."
    your brain reacts in the same way whether you are
    looking at something or thinking about it...

  4. #4
    SitePoint Enthusiast
    Join Date
    Sep 2006
    Posts
    26
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    so is there a function that will work on existing elements AND new elements? Kinda thought live served that purpose, it was explained to me as so…
    I think no.
    Just call bind() for existing elements after page loading.
    Then .live() for new elements in some event, eg. click event.
    ArticleQueen: First Article Rewriter with Lexical Processor!

  5. #5
    SitePoint Enthusiast
    Join Date
    Apr 2009
    Posts
    73
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Just FYI the .live() event will work on all new AND existing elements as stated in the jquery documentation:

    Decription: Attach a handler to the event for all elements which match the current selector, now or in the future.
    - see http://api.jquery.com/live/

    I would recommend (if you haven't already) adding your live call within a $(document).ready call.

    Good luck

  6. #6
    SitePoint Enthusiast
    Join Date
    Sep 2006
    Posts
    26
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Just FYI the .live() event will work on all new AND existing elements as stated in the jquery documentation:
    Oh, really? But doesn't seem to work for me.
    I have to call bind() in $(document).ready.
    ArticleQueen: First Article Rewriter with Lexical Processor!

  7. #7
    SitePoint Guru downtroden's Avatar
    Join Date
    Dec 2004
    Location
    illinois
    Posts
    972
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    the live() function IS in a .ready function. still doesn't work as advertised (on THIS project). Works as should on another project, that's why i'm confused.
    your brain reacts in the same way whether you are
    looking at something or thinking about it...


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
  •