I have a ul list.
I want to remove all li in my ul through with it id?
usually <ul> tag contains only <li> tags. If you want to remove all <li> tags then $("#ulelementID"). html(""); will work.
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 .
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With