0

The code below shows a rendered HTML (from Compiled ASP .Net code). I would not be able to make any changes to it including adding new attributes like ID.

I could still do something with Javascript but because there is no unique ID for the column, I could not replace the underscore text "____________" with some other text.

The plan is to replace the underscore in column 3 for each row with some other text. Is there a way to identify Column 3 with Javascript?

Thank you.

    <table width='100%'>
        <tr>
            <td align="left" valign="top" class='clsReadOnly'>Row 1
            </td>
            <td align="left" valign="top" class='clsReadOnly'>
                abc
            </td>
            <td align="left" valign="top" class='clsReadOnly'>
                __________________________
            </td>
        </tr>
        <tr>
            <td align="left" valign="top" class='clsReadOnly'>Row 2
            </td>
            <td align="left" valign="top" class='clsReadOnly'>
                def
            </td>
            <td align="left" valign="top" class='clsReadOnly'>
                __________________________
            </td>
        </tr>
        <tr>
            <td align="left" valign="top" class='clsReadOnly'>Row 3
            </td>
            <td align="left" valign="top" class='clsReadOnly'>
                ghi
            </td>
            <td align="left" valign="top" class='clsReadOnly'>
                __________________________
            </td>
        </tr>
    </table>

3 Answers 3

2

If this is the only table on the page, you can do it with jQuery:

$('table')
    .children('tr')
    .each(function() {
        $(this).children('td:last').html('<p>A Paragraph of Text</p>');
    });
Sign up to request clarification or add additional context in comments.

2 Comments

Same as: $('table > tr > td:last').html('<p>A Paragraph of Text</p>');
@box9: Your code is smooth, and readable. However, my example would resolve faster. Using jQuery functions like .children() to filter and traverse the DOM is more efficient than relying on the selector engine to do it for you.
1

It depends on what you are trying to accomplish. If this is how your table will always look, you could do the following:

var cells = document.getElementsByClassName('clsReadOnly');

cells[2].innerHTML = "Row1 Column3";
cells[5].innerHTML = "Row2 Column3";
cells[8].innerHTML = "Row3 Column3";

If you don't know how many columns you will start with, you would have to do this:

var rows = document.getElementsByTagName('tr');

for (var i=0; i<rows.length; i++) {
    var cells = rows[i].getElementsByTagName('td');

    cells[2].innerHTML = 'Row'+(i+1)+' Column3';
}

jQuery would make this much easier, as you can select by tag, by parent, etc. Of course, you can do all of this in plain JavaScript, but it takes a lot more work.

http://jsfiddle.net/bShZa/

1 Comment

getElementsByClassName() is not supported in IE6, IE7, or IE8.
1

getElementsByClassName() is HTML5 and is not currently supported in IE. Here is the jsfiddle for the solution http://jsfiddle.net/V38MF/2/ w/o jQuery needed

var myTable = document.getElements('td');
for(var i=0; i<myTable.length; i++){
    if((i+1)%3 == 0)
        console.log(myTable[i].innerHTML);
}

2 Comments

You should put that code here instead of jsfiddle.net, for posterity.
true. posterity and readability makes code maintenance easy. you should try mootools instead of jquary for posterity and readability.

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.