1

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.

Russell
  • 655
  • 2
  • 9
  • 21
  • possible duplicate of [JavaScript: remove element by id](http://stackoverflow.com/questions/3387427/javascript-remove-element-by-id) – el Dude Sep 16 '13 at 10:55

1 Answers1

4

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);
Neeraj
  • 8,408
  • 8
  • 41
  • 69