5

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";};

2 Answers 2

9
obj.firstElementChild.nextElementSibling

(doesn't work in IE8)

Or

obj.children[1]
Sign up to request clarification or add additional context in comments.

2 Comments

obj.firstChild.nextSibling would get the first Element node assuming the HTML is as in the example (the text node containing the line break and the spaces is the first child).
Thank you SLaks, that obj.children[1] is a really useful thing, thank you again! :)
-3

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.

3 Comments

I know, and I will! : ) I just need time to get used to Javascript. I see all these things being done on other websites (with jQuery) and I AM SO JEALOUS! But, I'm just not there yet. ;P
easier doesn't mean always better.
The javascript tag says Unless a tag for a framework/library is also included, a pure JavaScript answer is expected

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.