I am appending li
in a ul
using the following code:
for (var i = 1; i <= len; i++) {
li = document.createElement(\'li\');
$('#button').on('click', function() {
$(root).empty();
});
What about?
var ul = root;
ul.innerHTML = '';
You need to fetch the elements before removing them as the native DOM methods (most of them anyway) can't be passed in selector strings the same way jQuery's methods can.
var lis = root.getElementsByTagName("li");
for(var i = 0, il = lis.length;i<il;i++) {
root.removeChild(lis[i]);
}
You appear to be trying this with raw JavaScript:
while( root.firstChild ){
root.removeChild( root.firstChild );
}
jQuery will only slow you down here.
document.getElementById("the_ul_ID").innerHTML = "";
If you are using jQuery, why don't you use it's benefits?
adding <li>
elements:
$("<li><img src='"+path[i]+"'></li>").appendTo(root);
removing all <li>
elements:
$(root).empty();
deleting one <li>
element:
$("li:eq(3)",$(root)).remove();
and if you are using raw js, you can use:
document.getElementById("root").innerHTML = "";