5

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 { ... } ?

1 Answer 1

7

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]. ...
}
Sign up to request clarification or add additional context in comments.

2 Comments

This seems to resolve it. Thank you for your help (edit+answer)
@Tbi45 But pay attention that the described approach won't work in IE<8. Then such methods as document.getElementById() or element.getElementsByTagName() must be applied.

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.