Jump to content

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.