vusan vusan - 1 year ago 92
Javascript Question

td without parent table tag


<td class="tabletd"> text one </td><br>
<td class="tabletd" id="tdSecond"> this is next td</td>
<button onclick="myFunc()">click</button>


function myFunc() {
var second = document.getElementById('tdSecond').innerHTML;
//var second2 = document.getElementsByTagName('td')[1]; //gives error undefined

I cannot make this work. Even there is no effect of css see JSFiddle.

But both javascript and css work fine if we use
without its parent

And also javascript and css work fine on custom tag. for eg:-
<ddd> text </ddd>

So why we get error on both css and javascript if we use
without its parent

Answer Source

If you inspect the element in chrome you will notice the code has changed to the following:

    text one <br>
    this is next td
    <button onClick="myFunc()">click</button>
    <script type="text/javascript">//<![CDATA[ 

    function myFunc() {
    var second=document.getElementById('tdSecond').innerHTML;

    //var second2=document.getElementsByTagName('td')[1].innerHTML;



The td elements have been stripped by the browser as they are not valid.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download