Jump to content

Delete rows then insert


phpgoal

Recommended Posts

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>
Link to comment
https://forums.phpfreaks.com/topic/279566-delete-rows-then-insert/
Share on other sites

 

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.