dom - Target is null error in browser dev tools -
i trying insert newly created elements (rows , cells) table (via javascript) above row has id of "dynpop". when attempt this, following error
typeerror: target null
here html of table...
<table> <tr id="dynpop"> <td style="color:white"> blah blah blah blah blah </td> </tr> </table>
and here code insert new row , cell before it...
$rownumber = 1; var target = document.getelementbyid('dynpop'); //populate member table chapter image row var $chap_image = $out[1]; var olr1 = document.createelement('tr'); var old1 = document.createelement('td'); var olt1 = document.createtextnode($chap_image); old1.appendchild(olt1); olr1.appendchild(old1); $rowname = 'row' + $rownumber; olr1.setattribute('name', $rowname); target.insertbefore(olr1, target); $rownumber++;
any thoughts why javascript code not working ?
thanks!
i"m not sure without seeing rest of html, think main problem javascript executing before dom loads.
you might try putting javascript function gets called on body element's onload
event:
<!doctype html> <html> <head> <title>asdf</title> <script type="text/javascript"> function dothing() { $rownumber = 1; var target = document.getelementbyid('dynpop'); var $chap_image = "$out[1]; "; var olr1 = document.createelement('tr'); var old1 = document.createelement('td'); var olt1 = document.createtextnode($chap_image); old1.appendchild(olt1); olr1.appendchild(old1); $rowname = 'row' + $rownumber; olr1.setattribute('name',$rowname); target.insertbefore(target, olr1); $rownumber++; } </script> </head> <body onload="dothing()"> <table> <tr id="dynpop"> <td style="color:white"> blah blah blah blah blah </td> </tr> </table> </body> </html>
Comments
Post a Comment