Forum Moderators: open

Message Too Old, No Replies

Only Load JavaScript onclick of a Button

         

cookiemonster

6:08 pm on Dec 21, 2009 (gmt 0)

10+ Year Member



I have a JavaScript that uses document.getElementById to write something to the page.
My problem is that the JavaScript writes to the page onload. I want the <span> that it writes to to be blank until you click a button.

Here's what I have so far:

index.html:


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Random Generator</title>
<style type="text/css">
span {padding:15px 0; display:block; font: 1.5em 'Courier New',Courier,monospace}
</style>
</head>
<body>
<div align="center">
<form>
<input value="Generate!" type="submit" style="font-size:16px; font-weight:bold"/>
</div><br>
<span id="result" align="center"></span>
</form>
<script type="text/javascript" src="Random.js"></script>
</body>
</html>

Random.js:


function text(){
}
text = new text();
number = 0;
text[number++] = "one"
text[number++] = "two"
text[number++] = "three"
text[number++] = "four"
text[number++] = "five"
increment = Math.floor(Math.random() * number);
document.getElementById('result').innerHTML = (text[increment]);

Any thoughts?
Thanks.

Fotiman

6:56 pm on Dec 21, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



1. Give the button that you want to assign this behavior to some identifier. For example:

<input type="button" id="generate" value="Generate!" />

2. Modify Random.js:


(function () {
var generate = document.getElementById('generate');
generate.onclick = function () {
var text = [
"one",
"two",
"three",
"four",
"five"
],
inc = Math.floor(Math.random() * text.length);
document.getElementById('result').innerHTML = text[inc];
}
})();

Note, this example defines an event handler for the button. A better solution would be to define an event listener.

Fotiman

7:00 pm on Dec 21, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Note also that I greatly simplified Random.js. There is no need to use "new" to create the text variable, and you had both a function and variable with the same name. Also, text was essentially just an array, so I simplified that as well. Last, I wrapped the entire thing in:
(function () {
})();

In other words, I defined a function and then execute it immediately. This allows the variables inside to be scoped to this function only (so they don't cause potential conflicts with other scripts).
Hope that helps.

cookiemonster

7:08 pm on Dec 21, 2009 (gmt 0)

10+ Year Member



Thank you fotiman!

Saved me probably about a week of trying different stuff, plus this eliminates a previous problem I had which I had decided was just impossible to fix!

cookiemonster

7:27 pm on Dec 21, 2009 (gmt 0)

10+ Year Member



Once again, thank you very, very much!

I'm still new to JavaScript so this was a really good lesson for me.