Russell
Russell

Reputation: 665

Removing a node from HTML

From reading here and elsewhere I would expect the following to remove the first div from the page.

<div class = "name">one</div>
<div class = "name">two</div>
<div class = "name">three</div>
<div class = "name">four</div>
<div class = "name">five</div>
<div class = "name">six</div>

var removeName = function(x,y) {
x.remove(y);
};

removeName(document.getElementsByClassName("name"),0);

I'm not sure what I have missed? Any help appreciated.

Upvotes: 1

Views: 50

Answers (1)

Neeraj
Neeraj

Reputation: 8532

selector.remove(n), removes the nth child in the selected object. Here as the div are in an array, and not the children of a specific parent, you need to remove the div which is on a specific index. so selectedObjects[n].remove() should be called.

This is what you are looking for:

var removeName = function(x,y) {
x[y].remove();
};

removeName(document.getElementsByClassName("name"),0);

Upvotes: 4

Related Questions