phpgoal Posted June 25, 2013 Share Posted June 25, 2013 Hello, I am trying to delete rows if rowcount > 0. then insert three more rows. That means, each time i click the button, it will compare whether rowcount is greater than 0 or not. If yes, it will delete all the rows and insert three more rows on the fly. If no, it will just insert three more rows. Everything is working except when row is greated than 0. When row is greater than 0, it deletes but do not insert rows unless I click the button again. <html><body><script> function rowinsert(){ var e = document.getElementById("year");var f = document.getElementById("month1"); var strSel = "The Value is: " + e.options[e.selectedIndex].value ; var strSel1 = "The Value is: " + f.options[f.selectedIndex].value ; var rowCount = document.getElementById('myTable').rows.length;//alert(rowCount);//document.getElementById("tableId").deleteRow(i -1); if (rowCount >0) { for($del=0; $del<4; $del++){//alert($del);document.getElementById("myTable").deleteRow(0);} } for($row=0; $row<3; $row++){var table=document.getElementById("myTable");var row=table.insertRow($row);var cell1=row.insertCell(0);var cell2=row.insertCell(1);cell1.innerHTML="New1";cell2.innerHTML="New";}} </script> <div> <select id="year"> <option value="2013">2013</option> <option value="2012">2012</option> </select> <select id="month1" > <option value="Jan">January</option> <option value="Feb">Feb</option> <option value="Mar">Mar</option> </select> <br/> <button onClick="rowinsert();">Get Selected Item</button></div> <table id="myTable" border="1"> </table></body></html> Quote Link to comment https://forums.phpfreaks.com/topic/279566-delete-rows-then-insert/ Share on other sites More sharing options...
philtyphil Posted June 26, 2013 Share Posted June 26, 2013 Hello, I am trying to delete rows if rowcount > 0. then insert three more rows. That means, each time i click the button, it will compare whether rowcount is greater than 0 or not. If yes, it will delete all the rows and insert three more rows on the fly. If no, it will just insert three more rows. Everything is working except when row is greated than 0. When row is greater than 0, it deletes but do not insert rows unless I click the button again. <html><body><script> function rowinsert(){ var e = document.getElementById("year");var f = document.getElementById("month1"); var strSel = "The Value is: " + e.options[e.selectedIndex].value ; var strSel1 = "The Value is: " + f.options[f.selectedIndex].value ; var rowCount = document.getElementById('myTable').rows.length;//alert(rowCount);//document.getElementById("tableId").deleteRow(i -1); if (rowCount >0) { for($del=0; $del<4; $del++){//alert($del);document.getElementById("myTable").deleteRow(0);} } for($row=0; $row<3; $row++){var table=document.getElementById("myTable");var row=table.insertRow($row);var cell1=row.insertCell(0);var cell2=row.insertCell(1);cell1.innerHTML="New1";cell2.innerHTML="New";}} </script> <div> <select id="year"> <option value="2013">2013</option> <option value="2012">2012</option> </select> <select id="month1" > <option value="Jan">January</option> <option value="Feb">Feb</option> <option value="Mar">Mar</option> </select> <br/> <button onClick="rowinsert();">Get Selected Item</button></div> <table id="myTable" border="1"> </table></body></html> Try to using table tr last Quote Link to comment https://forums.phpfreaks.com/topic/279566-delete-rows-then-insert/#findComment-1437910 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.