javascript - why is it giving error "appendChild is null" error and how do i fix it -
when trying append td
element tr
, throwing error:
cannot read property appendchild of null
this code:
var col = prompt('enter number of columns, table needs have'); var row = prompt('enter number of rows, table should have'); function columncreator() { var table = document.queryselector('tablediv'); var tablerow = document.createelement('tr'); table.appendchild(tablerow); (var = 0; < col; i++) { var tabledata = document.createelement('td'); tablerow.appendchild(tabledata); var dummytext = document.createtextnode('table data'); tabledata.appendchild(dummytext); } } columncreator();
var table = document.queryselector('tablediv'); console.log(table)//undefined
theres nothing tablediv, either id,class, or nested structure:
var table = document.queryselector('#tablediv') //id || document.queryselector('.tablediv') //class || document.queryselector('table > div') //div inside table || document.queryselector('div > table');//table inside div
Comments
Post a Comment