javascript - Check for duplicated names in a list element -
question:
how check duplicated names/items in list element?
my situation:
i've got list within list, here example:
<ol class="sortable ui-sortable"> <li id="category_1"><div>car</div> <ol> <li id="category_2"><div>color</div> <ol> <li id="category_3"><div>red</div></li> <li id="category_4"><div>black</div></li> </ol> </li> </ol> </li> <li id="category_5"><div>motor</div> <ol> <li id="category_6"><div>red</div></li> <li id="category_7"><div>black</div></li> </ol> </li> <li id="category_9"><div>truck</div></li> </ol>
the class="sortable ui-sortable"
being used nestedsortable jquery plugin. list items dragable.
i want check names within divs, when div dragged list <ol>
. if list contains duplicate, highlight in red.
for exampe if drag red
- category_6
ol
within color
- category_2
, allready contains red, place (dropspot) turns red. if user decides still drop on place, list returns original position. category_6
moved ol
beneath category_5
.
so list end this, or else if have better way:
<ol class="sortable ui-sortable"> <li id="category_1"><div>car</div> <ol> <li id="category_2"><div>color</div> <ol> <li id="category_3"><div>red</div></li> <li id="category_4"><div>black</div></li> <li id="category_6" class="highlighted"><div>red</div></li> </ol> </li> </ol> </li> <li id="category_5"><div>motor</div> <ol> <li id="category_7"><div>black</div></li> </ol> </li> <li id="category_9"><div>truck</div></li> </ol>
considering list
<ul id="myid"> <li>apple</li> <li>ibm</li> <li>apple</li> <li>microsoft</li> <li>ibm</li> </ul>
the script finding duplicates suggest
var litext = '', lilist = $('#myid li'), listofduplicate = []; $(lilist).each(function () { var text = $(this).text(); if (litext.indexof('|' + text + '|') == -1) litext += '|' + text + '|'; else listofduplicate.push($(this)); }); $(listofduplicate).each(function () { alert("duplicates : " + $(this).text()); });
example using fiddle
Comments
Post a Comment