...

View Full Version : Question about working with onclick()



Savaries
12-14-2007, 10:42 PM
Im just getting into javascript and am wondering if it is passable to have a single
button on a page, lets call it roll. Is there a way for me to set it up
so that each time i push that button it will run a function, which is get a random number from 1 - 12 but leave any previous number on the screen. sorta like this

No clicks yet
(RUN)

One Click
(RUN)
2

Two Click
(RUN)
2
4

Sorry for the diagram just trying to make my question as clear as possible.

oesxyl
12-15-2007, 04:00 AM
<script type="text/javascript">
function roll(){
var val = '';
var textobj = document.getElementById('rollid');
if(textobj){
val = textobj.innerHTML;
}
val = val + ' ' + Math.floor(Math.random()*12+1);
textobj.innerHTML = val;
}
</script>

<input type="button" value="Roll" onclick="roll();"/>
<div id="rollid"></div>


best regards

Savaries
12-16-2007, 08:29 AM
dude thank you soo much. I finished most of the program but for some reason i just couldnt get that bit there.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum