Hi everyone,
Actually, i got “c.replace is not a function” while i was trying to delete some DOM elements and..i don’t understand.
i’d like to delete some
var liste=document.getElementById("tabs").getElementsByTagName("li");
for(i=0;i<liste.length;i++)
{
if(liste[i].id==2)
{
$("#tabs").detach(liste[i]);
}
}
I tried .detach and .remove but it’s the same. My version of jQuery is 1.7.1.min.js.
Thanks for help.
order of iteration on a NodeLIst
Doing forward iteration of a NodeList that is being modified when you remove an element can be an issue. Iterate in reverse when removing elements from the DOM.
misuse of
detach()Also, the arguments to
.detach()do not perform a nested find, but rather act as a filter on the existing element(s) in the jQuery object, and should be passed a string. It seems that you actually want to detach theli, which would mean that you’d need to call.detach()on theliitself…remove()may be preferredKeep in mind that if you use
.detach(), any jQuery data is retained. If you have no further use for the element, you should be using.remove()instead.code reduction
Finally, since you’re using jQuery, you could just do all this in the selector…
valid id attributes
Keep these items in mind with respect to IDs…
In the selector above, I used the attribute-equals filter, so it’ll work, but you should really be using valid HTML to avoid problems elsewhere.