Skip to content
loading...

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

github logo ・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!

twitter logo DISCUSS (3)
markdown guide
 

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 Oct 22 '18

How do you keep up with Kubernetes?

A collection of resources I use to stay up-to-date on Kubernetes

Vinh Le profile image

Sore eyes?

dev.to now has dark mode.

Go to the "misc" section of your settings and select night theme ❤️