9

I have a div which contain other tags inside it

<div id="mainDiv">
    <div>
        <table>
            <tbody>
                   <tr>
                      <td>1</td>
                      <td>item</td>
                      <td>item</td>
                      <td>2</td>
                   </tr>
            </tbody>
        </table>
    </div>

    <div>
        <table>
            <tbody>
                   <tr>
                      <td>1</td>
                      <td>item</td>
                      <td>item</td>
                      <td>5</td>
                   </tr>
            </tbody>
        </table>
    </div>
</div>

How can I access <td> of this mainDiv through javascript. I want to change innerHTML of these <td>

4 Answers 4

14
var allDivTd = document.getElementById("mainDiv").getElementsByTagName("TD");

for(var i = 0; i < allDivTd.length; i++){
    var td = allDivTd[i];
    td.innerHTML = // do something w/ inner html
}
Sign up to request clarification or add additional context in comments.

Comments

3

Using jQuery: $('div#mainDiv td') will return a set with all <td>s in it.

You can use .html() to modify the content of them. See http://jsfiddle.net/StuperUser/vD3Tk/ for an example.

Use jQuery if you're doing a lot of JS and have a lot of DOM manipulation to do. It has powerful and terse selector syntax from CSS, a lot of extension methods for DOM manipulation and cross browser compatibility. However, if you're only doing a small amount of JS, then don't feel it's necessary.

6 Comments

Edit your answer - you need a few quotes :)
It's good to refer to jquery for these kinds of situation. However, I would have +1'ed you if you give him the reasons to use jquery.
Or in Prototype.js $$('div#mainDiv td')
@Sayem Ahmed, Brian's answer is accurate for the OP, since if he's only manipulating the table, using the jQuery library may be overkill. If he's doing a lot of JS then I've included some of the best reasons for using jQuery.
@StuperUser should be echoed a thousand times from the highest mountain. jQuery is not a substitute for JavaScript. It is a high demand framework that simplifies difficult tasks and chaining in JavaScript, and is not applicable to all scenarios. I find there are far too many folks out there that "know" jQuery and don't know JavaScript - a scenario I'd have thought to be impossible just a few years ago.
|
2

You can use document.getElementsByTagName to get all the tr tags, and then iterate over them for accessing the individual tds:

trs = document.getElementsByTagName('tr');
for (var i = 0; i < tr.length; i++) {
    var tds = trs[i].childNodes;
        for (var J = 0; j < tds.length; j++) {
            var td = tds.childNodes[j];
            // process td
        }
    }
}    

Although , as you can see, this doesn't look to nice and is quite verbose. It's easier to use a Javascript framework such as jQuery, mootools, dojo... for these kinds of tasks. They support CSS selectors (e.g. jQuery) that let you traverse the DOM using CSS selectors, which are similar to XPath-style expressions and much more powerful than manual dom traversal with the few functions that Javascript originally provides for this.

Comments

1
var children = document.getElementById('mainDiv').getElementsByTagName('td');

Comments

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.