0

Simple question, since version 1.7 of jQuery .live() has been deprecated in favor of .on(); however .on() does not seem to work on elements that are rendered via JavaScript and loaded into the DOM. So my question is, should .live() still be used or how would .on() capture these newly generated elements?

As an example, here's my code:

$("#listitem").append("<li id='removeitem'>" + 
       formdata + ' <a href="#">Remove</a></li>');

And when I try to operate on this element via .on() - the result is nothing, whereas .live() is able to grab this element.

$("#removeitem").live("click", function(event) { alert($(this).text()); });
6
  • 2
    where is code for .on() you've tried? Commented Jun 2, 2012 at 19:58
  • All you need to know is here: Differences Between jQuery .bind() vs .live() vs .delegate() vs .on() Commented Jun 2, 2012 at 20:00
  • @thecodeparadox maybe that's my problem - I just replaced .live with .on Commented Jun 2, 2012 at 20:03
  • 1
    @firedrawndagger. Well... that's won't work for sure... Commented Jun 2, 2012 at 20:04
  • I got that and also suggest in answer Commented Jun 2, 2012 at 20:04

2 Answers 2

7

live version:

$("#removeitem").live("click", function(event) { alert($(this).text()); });

should be changed to:

$("#containerId").on("click", "#removeitem", function(event) { 
    alert($(this).text()); 
});

Where containerId is the static element that the removeitem is loaded into.

You can simply use body as the static element:

$("body").on('click', '#removeitem'm fn);

But the code will be less efficient, just try with it to show how it works.


Notes:

  1. I hope you don't have multiple elements with the same id because it's invalid markup.
  2. It looks like listitem is the static element in your code.

Update:

Maybe that's my problem - I just replaced .live with .on

You can't simply replace them... They work in other ways, and have different parameters!

Read the docs for each function:

on:

.on(events [, selector] [, data], handler(eventObject)) 

live:

.live( events, handler(eventObject) )
Sign up to request clarification or add additional context in comments.

7 Comments

Right the id is just something I put here, in reality I have a dynamically generated class that I assign, e.g. "item1", "item2", etc.
Now what if these are dynamic elements though, in other words I wouldn't know in advance if there were 2 or 5 of them, so how would I write the #removeitem, if it's really more like .removeitem1 and .removeitem2?
You can use start with selectors like [class^=removeitem] which will select all element with classname starting with removeitem
@firedrawndagger. Give all the new elements a class, let's say item and use this $('body').on('click', '.item', handler);
But if I just want one of them, e.g. out of a list, only remove the one with class .removeitem3 and leave the others be?
|
1

You should try with.on()like this:

$('#listitem').on('click', '#removeitem', function() {
    alert($(this).text());
});

DEMO

If you don't have fixed id like removeitem and if its structure like removeitem1, removeitem1 etc then try:

$('#listitem').on('click', '[id^=removeitem]', function() {
    alert($(this).text());
});

1 Comment

I'm not sure you can inform the OP if he didn't comment to your answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.