473,387 Members | 1,641 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

onclick only works once

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
Nov 20 '08 #1
5 7727
On 2008-11-20 22:00, Steve wrote:
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
Nov 20 '08 #2
On 2008-11-20 22:10, Conrad Lender wrote:
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
Nov 20 '08 #3
Steve meinte:
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
Nov 20 '08 #4
On Nov 20, 10:17*pm, Conrad Lender <crlen...@yahoo.comwrote:
On 2008-11-20 22:10, Conrad Lender wrote:
>
You're pushing new values to the rolls array, but always read the same
10 values from the start.
Thanks Conrad.

Nov 20 '08 #5
rf

"Steve" <st***********@chello.atwrote in message
news:24***************************@news.chello.at. ..
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.
Nov 21 '08 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Mike | last post by:
In my previous post, I wrote: > ... > GOAL: (very simple) Provide a hyperlink which, when clicked, > calls a javascript function which opens a new URL. > ... > PROBLEM: The following code...
8
by: KS | last post by:
Just to show some code to show the consept. <img id="date" onclick="javascript:show_calendar();" src="/PlexSysWeb/images/show-calendar.gif" width=20 height=18 border=0> What i want the...
7
by: Jan Hoffman | last post by:
hi @all I have the following element in an ascx-file: <asp:TextBox id="name" runat="server">name</asp:TextBox> The textbox belongs to an application of a telefonebook. I want that if I...
13
by: Chris | last post by:
I can create Javascript confirm message boxes during page creation, etc adding them to the button attributes (many good posts on this!). But how can I add this event after the button is pressed? I...
9
by: Donius | last post by:
Hey everyone, i am doing some stuff where i'd like to pop up a little confirmation before a user clicks on a 'delete' link. Just trying to keep the markup clean, i added an attribute ...
3
by: Reinhold Schrecker | last post by:
Hi there, I am trying to generate a dynamic menu with JavaScript/DOM and have problems to set the onclick-attribute for my <a>-elements. The following code works fine in Opera and Mozilla but...
2
by: pedestrian via DotNetMonster.com | last post by:
I'm having an issue with the button click event handler. I follow this book example using the single-file method. It is a very simple page with a Button and a label: <script runat="server">...
13
by: alvin.yk | last post by:
Hi, Normally, a piece of code such as <a href="http://www.yahoo.com" onclick="alert('hello');return false;">link</a> will stop the browser from actually going to href's destination....
5
by: John Grandy | last post by:
I wrote a function to populate my dropdowns on the client-side. The function adds <optionelements to the <selectonly if none are initially present. I call the population function from either...
10
by: art | last post by:
Hi, I have a form with an image button like this: <td><input type='text' name='search' id='search' value='' size=30><BR><span class=body><center>Enter Email Address</span><BR></ td> <td><a...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.