Mike
Mike

Reputation: 2735

How to use JS innerHTML by class name and child of a class

Is it possible to do a document.getElementBy Class ("post[1] .classname").innerHTML ?

post[1] to be the second DIV with class='post' and I want the child .classname of this.

For example: I want to get the innerHTML of the second post => div class="inside".

<div class="post">Post 1
<div class="inside"></div>
</div>

<div class="post">Post 2
<div class="inside">//this here</div>
</div>

<div class="post">Post 3
<div class="inside"></div>
</div>

Upvotes: 1

Views: 894

Answers (2)

Ruan Mendes
Ruan Mendes

Reputation: 92274

But if you don't want to use jQuery, but are OK with only supporting later browsers, you can use document.querySelector. http://jsfiddle.net/mendesjuan/4hEwd/

document.querySelector('.post:nth-child(2) .inside').innerHTML

Here's a list of browsers that support it: http://caniuse.com/queryselector

Upvotes: 2

Anderson Pimentel
Anderson Pimentel

Reputation: 5757

Try using jQuery:

alert($('.post:nth-child(2) > div').html());

JSFiddle here.

Upvotes: 2

Related Questions