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 { ... }
?
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]. ...
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With