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!
Top comments (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 thestyle.display = block
the each of them individually. You can do that by using a loop, like this example bellow:or if you want this the last one to be visible:
Here is a Codepen showing that your code should work.
codepen.io/kellyjandrews/pen/MQwBxx