Apple

JQuery code to remove all spaces with one content but one

Let's 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 this did not work, I want the jQuery code to delete all (.fruit) gaps with one content, but keep one (if possible, the first), so I get the following:

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

thank

+3
source share
4 answers
 $("span.fruit:contains(Apple):not(:first)").remove();
+8
source
$('span.fruit').each(function(){
  return $('span.fruit:contains('+$(this).text()+'):not(:first)').remove();
})
+2
source
var temp = array[];

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

});
+1
source
temp = $('span:first').clone(true);
//remove all the span
$().html();
$().append(temp);
0
source

Source: https://habr.com/ru/post/1739798/


All Articles