0

Using this code, I need to remove the generated new element. It is not working. No JS errors are appearing in firebug.

$('.popular-list li a').live("click",function() //this will apply to all anchor tags
 { 
 var stuff = $(this).text();
 var hasDuplicate = false;
 $('#favoritesdrinks li').each( function(){
 if ($(this).text() === stuff ){
 hasDuplicate = true;
 return false;
 }
 });
 if (hasDuplicate ) {
 alert("Already Added") } 
 else { 
 $('#favoritesdrinks').append('<li>'+stuff+' --- <a href="javascript:;" class="remove">Remove Item </a> </li>'); 
 }
 });
Removal:
 $("a.remove").click(function() {
 $(this).fadeOut(500, function() { $(this).remove(); });
 });
redsquare
78.8k21 gold badges155 silver badges160 bronze badges
asked Aug 6, 2009 at 20:53

2 Answers 2

1

You need to use the .live event for the anchors with class remove. Also the context of this will be the anchor inside the anchor click therefore you need to use .parent() to fadeOut & remove the li

$('.popular-list li a').live("click",function() { 
 var stuff = $(this).text();
 var hasDuplicate = false;
 $('#favoritesdrinks li').each( function(){
 if ($(this).text() === stuff ){
 hasDuplicate = true;
 return false;
 }
 });
 if (hasDuplicate ) {
 alert("Already Added") } 
 else { 
 $('#favoritesdrinks').append('<li>'+stuff+' --- <a href="#" class="remove">Remove Item </a> </li>'); 
 }
 });
 $("a.remove").live('click', function(ev) {
 ev.preventDefault();
 $(this).parent().fadeOut(500, function(ev) { 
 $(this).remove(); 
 });
 });
answered Aug 6, 2009 at 20:59
Sign up to request clarification or add additional context in comments.

3 Comments

This causes a infinite loop of ev undefined errors although does work once
He will also need to use parent() on "var stuff = $(this).text();" because that is also matching A and not the LI
Again Thank you very much redsquare you are my savior
0
$("a.remove").click(function() { $(this).fadeOut(500, function() { $(this).remove(); }); });

That line is going to remove the A link, not the LI tag because you are using $(this)

answered Aug 6, 2009 at 20:59

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.