/    Sign up×
Community /Pin to ProfileBookmark

onclick event generating new page

The page is supposed to generate a single column table of random numbers in regards to the numbers entered into the text box. This table is supposed to go on the page with everything else. The numbers are being generated but it is being put onto a new page and isn’t being written to a table.

[CODE]<html>
<head>
<!–
In Class Lab 2 Extension – Random Number Table
Author: Robert Belliveau
Date: 3/29/2011
–>

<title>In Class Lab 1 – Random Number Table</title>

</head>

<body>

<p>This allows the user to enter 3 numbers into text boxes, the first two
integers are the range of the availabe random numbers. The third number
is the amount of random numbers to be generated and placed in a single column table. </p>
<form name=”random” method=”post” action=””>
<p>

<label>
<input type=”integer” name=”textfield” id=”min”>
Min<br>
</label>

<label>
<input type=”integer” name=”textfield2″ id=”max”>
</label>

<label>
Max<br>
<input type=”integer” name=”textfield3″ id=”randoms”>
</label>

Number of random integers
</p>

<p>
<label>
<input type=”button” name=”generate” id=”generate” value=”Generate” onclick=”genRand();”>
</label>
</p>
</form>

<p>&nbsp;</p>

<table border = “2”>

<script type=”text/javascript”>
function genRand() {
var max = document.random.max.value;

var min = document.random.min.value;

var range = max – min + 1;

var rands = document.random.randoms.value;

min *= 1;
rands *= 1;

for (var i = 1; i <= rands; i++) {
var randNum = min + range * Math.random();

randNum = Math.floor(randNum);

}
for (var i = 1; i <= rands; i++) {
var randNum = min + range * Math.random();
randNum = Math.floor(randNum);
document.write(“<tr>”);
document.write(“<td>” + randNum + “</td>”);
document.write(“</tr>”);

}

}

</script>

</table>

</body>

</html>[/CODE]

I attached the entire code because I honestly do not know where the errors are and also because it is not a very long program.

to post a comment
JavaScript

2 Comments(s)

Copy linkTweet thisAlerts:
@double91authorMar 30.2011 — ive narrowed the problem down to it most likely being my submit button causing it to generate a new page. the table is writing correctly just not into the right html document
Copy linkTweet thisAlerts:
@tirnaMar 30.2011 — document.write will overwrite the original page if called after the page has finished loading.

Try something like this:

[CODE]
<html>
<head>
<title>In Class Lab 1 - Random Number Table</title>
<script type="text/javascript">
function getRand(){
var minRand = new Number(document.getElementById('txtMin').value);
var maxRand = new Number(document.getElementById('txtMax').value);
var numRand = new Number(document.getElementById('txtNumRand').value);
if(isNaN(minRand) || isNaN(maxRand) || isNaN(numRand)){return;}
while(tblResultsO.rows.length > 0){
tblResultsO.deleteRow(0);
}
for(i=0; i < numRand; i++){
var curRand = minRand + (Math.floor(Math.random()*(maxRand-minRand+1)));
var newRow = document.createElement('tr');
var newCell = document.createElement('td');
newCell.appendChild(document.createTextNode(curRand));
newRow.appendChild(newCell);
tblResultsO.appendChild(newRow);
}
}
window.onload=function(){
document.getElementById('btnGenerate').onclick=getRand;
tblResultsO = document.getElementById('tblResults');
}
</script>
</head>
<body>
<p>This allows the user to enter 3 numbers into text boxes, the first two
integers are the range of the availabe random numbers. The third number
is the amount of random numbers to be generated and placed in a single column table.
</p>
<div>
<p>
<label>
<input type="text" name="textfield" id="txtMin" />
Min<br>
</label>
<label>
<input type="text" name="textfield2" id="txtMax" />
</label>
<label>
Max<br>
<input type="text" name="textfield3" id="txtNumRand" />
</label>
Number of random integers
</p>
<p>
<label>
<input type="button" name="generate" id="btnGenerate" value="Generate" />
</label>
</p>
</div>
<table><tbody id="tblResults"></tbody></table>
</body>
</html>
[/CODE]
×

Success!

Help @double91 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.15,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...