Connecting Tech Pros Worldwide Help | Site Map

onclick only works once

  #1  
Old November 20th, 2008, 09:05 PM
Steve
Guest
 
Posts: n/a
This simple script works first time but if you click a second time on
the button nothing happens. I expected that the roll function would be
executed again but it doesn't. Could someone explain why.

http://members.chello.at/stephen.jou...hotspots1.html

javascript is at
http://members.chello.at/stephen.jou...e/hotspots1.js

Thanks in advance
  #2  
Old November 20th, 2008, 09:15 PM
Conrad Lender
Guest
 
Posts: n/a

re: onclick only works once


On 2008-11-20 22:00, Steve wrote:
Quote:
This simple script works first time but if you click a second time on
the button nothing happens. I expected that the roll function would be
executed again but it doesn't. Could someone explain why.
function roll() {
^^^^
for(var i=0; i<10; i++) {
var roll = Math.floor(Math.random()*6+1);
^^^^

Oops :-)


- Conrad
  #3  
Old November 20th, 2008, 09:25 PM
Conrad Lender
Guest
 
Posts: n/a

re: onclick only works once


On 2008-11-20 22:10, Conrad Lender wrote:
Quote:
Oops :-)
Err, scratch that. Sorry.

for(var i=0; i<10; i++) {
var roll = Math.floor(Math.random()*6+1);
rolls.push(roll);
^^^^^^^^^^
}
for(var i=0; i<10; i++) {
var dice="die" + i;
document.getElementById(dice).innerHTML=rolls[i];
^^^^^

You're pushing new values to the rolls array, but always read the same
10 values from the start.


- Conrad
  #4  
Old November 20th, 2008, 10:05 PM
Gregor Kofler
Guest
 
Posts: n/a

re: onclick only works once


Steve meinte:
Quote:
This simple script works first time but if you click a second time on
the button nothing happens. I expected that the roll function would be
executed again but it doesn't. Could someone explain why.
>
http://members.chello.at/stephen.jou...hotspots1.html
>
javascript is at
http://members.chello.at/stephen.jou...e/hotspots1.js
>
Thanks in advance
You are constantly pushing values onto the rolls array, and feeding the
output always with the same first few entries. A quick run with a
debugger (e.g. Firebug) would have revealed the problem.

A

rolls = [];

at the beginning of the roll function should suffice. (The rest of the
code is ..er... could be better.)

Gregor
  #5  
Old November 20th, 2008, 10:05 PM
Steve
Guest
 
Posts: n/a

re: onclick only works once


On Nov 20, 10:17*pm, Conrad Lender <crlen...@yahoo.comwrote:
Quote:
On 2008-11-20 22:10, Conrad Lender wrote:
Quote:
>
You're pushing new values to the rolls array, but always read the same
10 values from the start.
>
Thanks Conrad.

  #6  
Old November 21st, 2008, 04:35 PM
rf
Guest
 
Posts: n/a

re: onclick only works once



"Steve" <stephen.young@chello.atwrote in message
news:24fa0$4925cfd0$3eb29844$23511@news.chello.at. ..
Quote:
This simple script works first time but if you click a second time on the
button nothing happens. I expected that the roll function would be
executed again but it doesn't. Could someone explain why.
It is being executed again. You are just putting exactly the same numbers
back there.


Closed Thread


Similar Threads
Thread Thread Starter Forum Replies Last Post
OnClick in Div only works when user clicks text Daniel Beardsley answers 2 April 2nd, 2006 09:55 AM
asp code only works once in a while Isabel answers 2 July 23rd, 2005 05:15 PM
How to initialize an array only once Mark Hannon answers 4 July 23rd, 2005 02:29 PM
Why does my function only work once? TheKeith answers 5 July 20th, 2005 12:30 PM