views:

153

answers:

4

Hello,

Say i have the following html:

<span class="fruit">Apple</span>
<span class="fruit">banana</span>
<span class="fruit">Apple</span>
<span class="fruit">Apple</span>
<span class="fruit">orange</span>

I tried different methods but it didn't work, I want a jQuery code to remove all (.fruit) spans with same content but keep one (the first if possible), so i will end up with the following:

<span class="fruit">Apple</span>
<span class="fruit">banana</span>
<span class="fruit">orange</span>

Thank you

+7  A: 
 $("span.fruit:contains(Apple):not(:first)").remove();
David Morton
I love it when this guys seems to speak selectors rather than writing it. loland of course love jquery which makes all this so easy. can easily imagine a speech recognition software that you say get all the span removed and keep one and just does this. hehe
XGreen
This works for the specific case, but not for the general case.
Lee
+1  A: 
var temp = array[];

$(".fruit").each(function(i) {
    var html = $($this).html();
    if($.inArray(html, temp)) {
        $($this).remove();
    }
    else {
        temp.push(html);
    }

});
Peeter
+2  A: 
$('span.fruit').each(function(){
  return $('span.fruit:contains('+$(this).text()+'):not(:first)').remove();
})
Lee
A: 
temp = $('span:first').clone(true);
//remove all the span
$().html();
$().append(temp);
thethanghn