1

there are some lists that has same classes. I want to show the first element from the lists

<ul class="datas">
    <li class="data-smh1">abc</li>
    <li class="data-smh1">dd</li>
    <li class="data-smh1">cc</li>
    <li class="data-sc33">abc</li>
    <li class="data-sc33">dd</li>
    <li class="data-nn61">abc</li>
    <li class="data-nn61">dd</li>
</ul>

result:

<ul class="datas">
    <li class="data-smh1">abc</li>
    <li class="data-sc33">abc</li>
    <li class="data-nn61">abc</li>
</ul>

I tried $('class[name^="data-"]', but I couldn`t find any solutions.
Any help, thanks.

BoltClock
  • 700,868
  • 160
  • 1,392
  • 1,356
aquaism
  • 43
  • 5
  • http://stackoverflow.com/questions/10558923/jquery-remove-duplicated-element http://stackoverflow.com/questions/2822962/jquery-remove-duplicate-elements – rahularyansharma Apr 06 '13 at 03:12

3 Answers3

5

Try

$(function(){
    $('.datas').children().each(function(i, v){
        var $this = $(this);
        $this.siblings('.' + $this.attr('class')).remove()
    });
})

Demo: Fiddle

Arun P Johny
  • 384,651
  • 66
  • 527
  • 531
0
<ul class="datas">
    <li class="data-smh1">abc</li>
    <li class="data-smh1">dd</li>
    <li class="data-smh1">cc</li>
    <li class="data-sc33">abc</li>
    <li class="data-sc33">dd</li>
    <li class="data-nn61">abc</li>
    <li class="data-nn61">dd</li>
</ul>

JQUERY

$(document).ready(function(){
    var seen = {};
    $('.datas li').each(function(){
    //alert($(this).text());
    var txt = $(this).attr('class');
     if (seen[txt])
        $(this).remove();
    else
        seen[txt] = true;

    })
  });

result

<ul class="datas">
    <li class="data-smh1">abc</li>


    <li class="data-sc33">abc</li>

    <li class="data-nn61">abc</li>

</ul>

JSFILDDEL LINK

rahularyansharma
  • 11,156
  • 18
  • 79
  • 135
  • You copied http://stackoverflow.com/questions/2822962/jquery-remove-duplicate-elements – Jashwant Apr 06 '13 at 03:30
  • i just use that logic but that is for duplicate element and this is for duplicate class name .. because all elements are same in result and one more thing i posted that link first in comment, after that i make this solution in jsfiddle. – rahularyansharma Apr 06 '13 at 03:32
0

you could try this:

$(document).ready(function(){
    var curr_class = '';
    var ctr = 0;    

    $('.datas li').each(function(){
        if (curr_class !== $(this).prop('class')){
            curr_class = $(this).prop('class');
            ctr = 0;
        }
        if (ctr !== 0 ){
            $(this).remove();
        }
        ++ctr;
    });

});

jsFiddle

Joseph Caracuel
  • 974
  • 1
  • 8
  • 15