I'm not sure if this is possible, I rarely do when I ask questions on this site, but everyone keeps telling me to use jQuery to do so. I make websites, but I am VERY novice at all of this and would prefer to get solid grip on Javascript before moving forward.
Anyways, my question is "Is it possible to target a second child (i.e.
<div>
<div></div>
<div></div> <!--This one-->
</div>
using JAVASCRIPT in order to manipulate it? If so, how?"
My previous attempt didn't work, I had a strong feeling it wouldn't but, all the same.
function activateEvent(obj) {
obj.secondChild.style.display="block";};
I'm not sure if this is possible, I rarely do when I ask questions on this site, but everyone keeps telling me to use jQuery to do so. I make websites, but I am VERY novice at all of this and would prefer to get solid grip on Javascript before moving forward.
Anyways, my question is "Is it possible to target a second child (i.e.
<div>
<div></div>
<div></div> <!--This one-->
</div>
using JAVASCRIPT in order to manipulate it? If so, how?"
My previous attempt didn't work, I had a strong feeling it wouldn't but, all the same.
function activateEvent(obj) {
obj.secondChild.style.display="block";};
Share
Improve this question
edited Aug 14, 2013 at 13:50
Hashem Qolami
99.6k27 gold badges155 silver badges165 bronze badges
asked Aug 14, 2013 at 13:37
Logan CersonLogan Cerson
771 silver badge7 bronze badges
2 Answers
Reset to default 9obj.firstElementChild.nextElementSibling
(doesn't work in IE8)
Or
obj.children[1]
You really should try to learn jQuery because it will make things a lot easier for you. Get the basics from plain Javascript and then learn a library like jQuery. you just need to load it into your website, then you can target the element in your exemple using: $('.parent div:nth-child(2)')
It's that easy.