Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
175
rated 0 times [  178] [ 3]  / answers: 1 / hits: 46018  / 12 Years ago, sat, january 12, 2013, 12:00:00

I'm trying to remove the first li in an ol using the DOM removeChild(). But for some reason it doesn't work.



This is my javascript:



document.getElementById('queue').removeChild(
document.getElementById('queue').childNodes[0]
);


And this is my HTML:



<ol id=queue>
<li>Surprised Kitty (Original)<span class=nodisplay>0Bmhjf0rKe8</span></li></ol>


I tried alerting the childNodes[0], and it returns [Object Text], which seems a bit weird, when I was expecting just the object.



Hope I've been clear.


More From » element

 Answers
12

Between the <ol id=queue> and the <li> tag are spaces and a line break. These make up a text node. The first child of the #queue element is therefore a text node.



You can use the .children property instead of .childNodes, it only considers element nodes, or iterate over all child nodes until you find the first li node, like suggested by dystroy.


[#80917] Friday, January 11, 2013, 12 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
abagail

Total Points: 528
Total Questions: 109
Total Answers: 101

Location: Western Sahara
Member since Mon, May 3, 2021
3 Years ago
;