I am trying to add link text inside <li>to <ul>. The problem is that he is trying to scan and find duplicates, not add a new one
.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 IS THE 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>');
}
});
source
share