Reveal the nth child of a div with id ‘hello’

・1 min read

Hi all,
I am getting stuck on how to reveal the nth child of a div with id ‘hello’ by pure JavaScript. I actually tried:

var elements = document.getElementById(hello);
var child = elements.children[n-1];
child.style.display = block;

However, it doesn’t seem to be right. Would be great to hear how you are likely to solve this problem! Thank you!

DISCUSSION (3)
 

The code seems correct to me, assuming n is assigned previously. Any additional context into the output might help.

 

If you want to make all the elements inside of hello visible, you need to set the style.display = block the each of them individually. You can do that by using a loop, like this example bellow:

var elements = document.getElementById('hello');
for (i=0; i<elements.children.length; i++) {
  var child = elements.children[i];
  child.style.display = 'block';
}

or if you want this the last one to be visible:

var elements = document.getElementById('hello');
elements.children[elements.children.length-1].style.display = 'block';
 
Classic DEV Post from Feb 13

How Do I Start Giving Talks on Coding?

I don't consider myself an expert in just about any aspect of computer science ...