[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

I need to remove a list item from the DOM using jQuery

Posted on 2011-10-13
6
Medium Priority
?
256 Views
Last Modified: 2012-05-12
I have the HTML variable and have validate it does in fact have duplicate error messages. I need to remove duplicates. I need to remove the duplicate list item (li) from the ul in the DOM using jQuery or JavaScript .

How do I do that?

Thanks!
0
Comment
Question by:newbieweb
6 Comments
 
LVL 11

Assisted Solution

by:Kusala Wijayasena
Kusala Wijayasena earned 300 total points
ID: 36964978
0
 
LVL 12

Accepted Solution

by:
zappafan2k2 earned 800 total points
ID: 36965547
It's one thing to remove a list item.  It's another to remove duplicates.

I can't say that this is the best or most efficient way, but I would tackle this like

<ul id="errors">
  <li>msg1</li>
  <li>msg2</li>
  <li>msg3</li>
  <li>msg1</li>
  <li>msg4</li>
  <li>msg2</li>
</ul>

<script type="text/javascript">
$(document).ready(function() {
    var $ul = $('#errors');
    var items = {};    // empty object
    $ul.find('li').each(function() {
        var msg = $(this).text();
        items[msg] = 1;
        $(this).remove();
        });        // now you have an empty <ul> and an object with unique keys
    for (i in items) {
        $('<li></li>').text(i).appendTo($ul);
        }
    });
</script>

Open in new window

0
 
LVL 40

Assisted Solution

by:Gurvinder Pal Singh
Gurvinder Pal Singh earned 600 total points
ID: 36966690
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 40

Assisted Solution

by:Gurvinder Pal Singh
Gurvinder Pal Singh earned 600 total points
ID: 36966692
0
 

Assisted Solution

by:pepd
pepd earned 300 total points
ID: 36969539
if dublicate variable(s) are even or odd ordered -assuming even- you can try;
$("ul#YourUlId:even").find("li:even").remove()

for better explanation you of remove() function have a look at: http://api.jquery.com/remove/
0
 

Author Closing Comment

by:newbieweb
ID: 36972147
Thanks!
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A while back, I ran into a situation where I was trying to use the calculated columns feature in SharePoint 2013 to do some simple math using values in two lists. Between certain data types not being accessible, and also with trying to make a one to…
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
In this Micro Tutorial viewers will learn how to create navigation buttons that change on rollover, using CSS (Continuation of the CSS Image Sprite tutorial) Create a parent ID for all the list items       - Specify position: absolute and display: block…
In this tutorial viewers will learn how to embed an audio file in a webpage using HTML5. Ensure your DOCTYPE declaration is set to HTML5: : The declaration should display (CODE) HTML5 is supported by the most recent versions of all major browsers…
Suggested Courses

872 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question