Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Access the inner DIV with JavaScript

If I have the following HTML:

<tr class="class">
    <td>
       <div>
       </div>
    </td>
</tr>

How can I access the div with JavaScript knowing that all the styles on the div are applied like this: .class td div { ... } ?

like image 822
Tbi45 Avatar asked May 20 '13 11:05

Tbi45


1 Answers

For modern browsers querySelector() is the way to go:

var html = document.querySelector(".class td div").innerHTML;

For accessing multiple elements you can use querySelectorAll():

var elements = document.querySelectorAll(".class td div");
for (var i = 0, len = elements.length; i < len; i++) {
    // elements[i]. ...
}
like image 171
VisioN Avatar answered Oct 11 '22 19:10

VisioN