Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

delete row of a table using randon number

Status
Not open for further replies.

cathiec

Programmer
Oct 21, 2003
139
IE
i am trying to add a random number function that will generate a random number and then delete the row of the table corresponding to a cell in the row that contains the random number. here is the code that i am working on, it works some times but not all the time:

function deleteJobs(t){
var tbl = document.getElementById(t);
for(var i = tbl.rows.length-3; i >=0; i--){
var ran_number = Math.floor(Math.random( )* 4);
ran_number = ran_number + "";
//alert(ran_number);
if(tbl.rows.cells[3].innerText.indexOf(ran_number)>-1){
tbl.deleteRow(i);
}
}
}



 
This is an odd one... But perhaps you need to use the number of rows in the ran_number equation....



function deleteJobs(t){
var tbl = document.getElementById(t);
var numRows = tbl.getElementsByTagName("tr").length
for(var i = tbl.rows.length-3; i >=0; i--){
var ran_number = Math.floor(Math.random( )* numRows);
ran_number = ran_number + "";
//alert(ran_number);
if(tbl.rows.cells[3].innerText.indexOf(ran_number)>-1){
tbl.deleteRow(i);
}
}
}


Programming today is a race between software engineers striving to build better and bigger idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. - Rick Cook (No, I'm not Rick)

fart.gif
 
that works great but still sometimes a row corresponding to the random number is not deleted.
 
is the random number always in the 4th cell?

Why not remove the row based on it's number (rather than a number stored in a cell). For instance. If you have a table with 6 rows, you generate a number between 0 and 5 and delete the corresponding row. Since it's random, why make it match a number in a cell?

Programming today is a race between software engineers striving to build better and bigger idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. - Rick Cook (No, I'm not Rick)

fart.gif
 
yep, the random number is going to be a number between 1 and 10 and each cell 4 will be numbered consecutively from 1 to 10. the code i am working on at the moment is below. what i want it to do is to always show 5 rows of the table. what it does at the moment is that it does delete rows based on the random number but sometimes it doesnt delete any rows when the button is pushed and other times it deletes one or two rows.

function deleteJobs(t){
var tbl = document.getElementById(t);
var numRows = tbl.getElementsByTagName("tr").length;
allNums = new Array(0,1,2,3,4,5,6,7,8,9);

for(var i = tbl.rows.length-1; i >=0; i--){
var thisOne = Math.floor(Math.random( )* allNums.length);
ran_number=allNums[thisOne];
allNums.splice(thisOne,1);
ran_number = ran_number + "";
alert(ran_number);
if(tbl.rows.cells[0].innerText.indexOf(ran_number)>-1){
tbl.deleteRow(i);
}
}
}
 
OK - so why not do as I suggested? The tables rows can be accessed using the values 0-9. When you delete one, the table rows can be access using values 0-8 regardless of the rownumber deleted. Your current problem is that a row can be deleted twice (even though it only exists once).

Programming today is a race between software engineers striving to build better and bigger idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. - Rick Cook (No, I'm not Rick)

fart.gif
 
<script>
function deleteRow(){
tObj = document.getElementById(&quot;myTable&quot;)
var numRows = tObj.getElementsByTagName(&quot;tr&quot;).length
var ran_number = Math.floor(Math.random( )* numRows);
tObj.deleteRow(ran_number)
}
</script>

<table id=&quot;myTable&quot;>
<tr><td>1</td></tr>
<tr><td>2</td></tr>
<tr><td>3</td></tr>
<tr><td>4</td></tr>
<tr><td>5</td></tr>
<tr><td>6</td></tr>
<tr><td>7</td></tr>
<tr><td>8</td></tr>
<tr><td>9</td></tr>
<tr><td>10</td></tr>
</table>
<input type=button onClick=&quot;deleteRow()&quot; value=&quot;Delete Random Row&quot;>

Programming today is a race between software engineers striving to build better and bigger idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. - Rick Cook (No, I'm not Rick)

fart.gif
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top