I am trying to append the text of a link inside a <li> to a <ul> The problem is getting it to scan through and find duplicates and not add a new
Here is the code:
$('.popular-list li a').live("click",function() //this will apply to all anchor tags
{
var stuff = $(this).text();
var match = $('#favoritesdrinks li:contains(' + stuff + ')');
if(match) {
alert("Already Added")
} else {
$('#favoritesdrinks').append('<li>'+stuff+'</li>');
}
}
);
UPDATE- THIS IS WORKING CODE:
$('.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+'</li>');
}
});
you will need to each through the li items. Then do the compare and if you have a match you can set the var to true then break out of the each using return false so you dont keep iterating when you already know the result.