Changing innerHTML of elements in nested div tag

It became apparent that I am quite an idiot - there was a bug in my JS in this code - sorry to bother you - everything below is correct in one form or another .. (also I choose the one that works in IE - correct answer) .. Thanks for your help !!

- Representative code only - I need to know how to change the innerhtml tablerow -

<html>
<head>
<script> //full javascript declaration - function call after page loads..
function ()
{
document.getElementById('tr1').innerHTML='<td>Test</td>'; // does not work..
document.getElementByTagNames('tr')[0].innerHTML='<td>Test</td>'; // does not work..
document.getElementById('div2').getElementsByTagNames('tr')[0].innerHTML='<td>Test</td>'; //does not work..
document.getElementById('div1').div2.tr1.innerHTML='<td>Test</td>'; //nope
}
</script>
</head>
<body>
<div id='div1'>
    <div id='div2'>
        <table>
            <tr id='tr1'></tr>
        </table>
    </div>
</div>
</body>
</html>

      

thanks for the help

Let me be clear .. I have 400 lines of HTML / JS / JQuery code that are dynamically generated, so I will simplify the task that is the problem

This function is called when the body is loaded:

function loadinitialData()
{
$('#tabs').tabs();
document.getElementById('tr0').innerHTML = '<td>John Doe</td><td>3.988000</td><td>Candidate Doe</td>';
document.getElementById('tr1').innerHTML = '<td>Jane Doe</td><td>5.796000</td><td>Candidate Doe</td>';
}

      

It's in the body:

<div id="tabs" class="ui-corner-top"> 
            <ul> 
                <li><a href="#tabs-1">Some Idiots</a></li> 
                <li><a href="#tabs-2">Other Idiots</a></li> 
            </ul>
            <div id="tabs-1">
            <table width="590px" cellpadding="4px" rules="groups" frame="void" bordercolor="white">
            <tr><td>Name</td><td>Time Taken</td><td>Something</td></tr>
            <tbody>
            <tr style='font-size:12px' id='tr0'></tr><tr style='font-size:12px' id='tr1'></tr></tbody></table></div>
            <div id="tabs-2">Phasellus mattis tincidunt nibh. Cras orci urna, blandit id, pretium vel, aliquet ornare, felis. Maecenas scelerisque sem non nisl. Fusce sed lorem in enim dictum bibendum.</div> 
</div>

      

PS: Using jquery tabs, but that can't be a problem.

Per @Gero I tried the following: Changed the whole download:

document.getElementById('tablet10').rows[0].cells[0].innerHTML = 'John Doe';
document.getElementById('tablet10').rows[0].cells[1].innerHTML ='3.988000';
document.getElementById('tablet10').rows[0].cells[2].innerHTML ='Candidate Doe';

      

Blah Blah - everything else ..

In body

<div id="tabs-1"> 
            <table id="tablet10" width="590px" cellpadding="4px" rules="groups" frame="void" bordercolor="white"> 
            <tr><td>Name</td><td>Time Taken</td><td>Something</td></tr> 
            <tbody> 
            <tr style='font-size:12px'><td></td><td></td><td></td></tr><tr style='font-size:12px'><td></td><td></td><td></td></tr></tbody></table></div> 

      

To download @plodder:

document.getElementById('tr0').insertCell(0).innerHTML = 'John Doe';
document.getElementById('tr0').insertCell(1).innerHTML ='3.988000';
document.getElementById('tr0').insertCell(2).innerHTML ='Candidate Doe';

      

and the rest blah blah ..

and body:

<div id="tabs-1"> 
            <table width="590px" cellpadding="4px" rules="groups" frame="void" bordercolor="white"> 
            <tr><td>Name</td><td>Time Taken</td><td>Something</td></tr> 
            <tbody> 
            <tr style='font-size:12px' id='tr0'></tr><tr style='font-size:12px' id='tr1'></tr></tbody></table></div> 

      

+2


a source to share


3 answers


The code will not work in IE because TR.innerHTML cannot be written in IE

You have to use DOM API for table rows



document.getElementById('tr1').insertCell(myCell);

      

0


a source


You can see an example of working code here. http://www.w3schools.com/js/tryit.asp?filename=try_dom_tablerow_cells



0


a source


"The property is read / written for all objects except for the following, for which it is read-only: COL, COLGROUP, FRAMESET, HEAD, HTML, STYLE, TABLE, TBODY, TFOOT, THEAD, TITLE, TR."

"DHTML expressions may be used in place of the previous value (s)."

http://msdn.microsoft.com/en-us/library/ms533897.aspx

0


a source







All Articles