Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jquery remove duplicate li

<ul id="myid">   
<li>microsoft</li>  
<li>microsoft</li>  
<li>apple</li>  
<li>apple</li>  
</ul>   

I want to remove duplicates from li by using jquery.

How can I do that?

like image 640
dave Avatar asked Oct 01 '10 11:10

dave


People also ask

How do I remove duplicate values from a drop down list in HTML?

You also can remove the duplicates from the table list firstly, then create the drop down list. Select the column range you want to use in the table, the press Ctrl + C to copy it, and place it to another position by pressing Ctrl + V. Then keep selecting the list, click Data > Remove Duplicates.

How can we prevent duplicate values in multiple dropdowns in jquery?

The below codes works fine for two dropdowns, but not for more than 2. var $select = $("select[id$='go']"); $select. change(function() { $select . not(this) .

How can check duplicate value in textbox using jquery?

Using each() check value of inputs and if any value is duplicate add class duplicate to it.


2 Answers

I have used @Thariama solution in the past, but I have compatibility problems with IE6 (I still needs to support this dinosaur).

If the item repeats, so remove it from ul. It works with dynamic added li.

            var seen = {};
            $("ul#emails_exclusion_list").find("li").each(function(index, html_obj) {
                txt = $(this).text().toLowerCase();

                if(seen[txt]) {
                    $(this).remove();
                } else {
                    seen[txt] = true;
                }
            });
like image 187
eduardomozart Avatar answered Oct 18 '22 17:10

eduardomozart


example I find that the script is faster

var liText = '', liList = $('#myid li'), listForRemove = [];

$(liList).each(function () {

  var text = $(this).text();

  if (liText.indexOf('|'+ text + '|') == -1)
    liText += '|'+ text + '|';
  else
    listForRemove.push($(this));

})​;

$(listForRemove).each(function () { $(this).remove(); });
like image 44
andres descalzo Avatar answered Oct 18 '22 17:10

andres descalzo