Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New to the CF scene
    Join Date
    Dec 2007
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question about working with onclick()

    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.

  • #2
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    Code:
     <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

  • #3
    New to the CF scene
    Join Date
    Dec 2007
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts
    dude thank you soo much. I finished most of the program but for some reason i just couldnt get that bit there.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •