Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to remove all li in my ul through with it id?

I have a ul list.

I want to remove all li in my ul through with it id?

like image 304
w3father Avatar asked Apr 27 '11 09:04

w3father


People also ask

How do I clear ul li?

usually <ul> tag contains only <li> tags. If you want to remove all <li> tags then $("#ulelementID"). html(""); will work.

How do I remove an element from UL?

First, get the ul element with the id menu by using the getElementById() method. Then, remove the last element of the ul element by using the removeChild() method. The menu. lastElementChild property returns the last child element of the menu .

How do I remove an item from a list in HTML?

HTML consists of both an element node and a text node. So createTextNode() method creates a text node with the specified text. Remove existing element: We can remove a child from the created list by using removechild() function.

How do you append UL?

Answer: Use the jQuery append() Method You can simply use the jQuery append() method to add <li> elements in an existing <ul> element. The following example will add a <li> element at the end of an <ul> on click of the button.


1 Answers

All that jQuery stuff. :-)

A plain old javascript version:

  var ul = document.getElementById('<ul id>');
  if (ul) {
    while (ul.firstChild) {
      ul.removeChild(ul.firstChild));
    }
  }

You could also set:

  ul.innerHTML = '';

or even:

  ul.parentNode.replaceChild(ul.cloneNode(false), ul);

but I prefer the first method.

like image 138
RobG Avatar answered Oct 05 '22 04:10

RobG