HumbleApprentice Posted August 4, 2013 Share Posted August 4, 2013 Hi Guys, I am teaching myself JavaScript. This little program works but I am unable to pass the result to onclick. Could you please help. Thanks. <!doctype html> <html> <h2> 12 Times Table </h2> <script> function 12Times() { var times=1 for (times =1;times<=12; times++) alert(times + " times 13 is " +(times * 13) + "<br>"); } </script> <body> <input type="button" onclick="12Times()" value="12Times"> </body> </html> Link to comment Share on other sites More sharing options...
Don E Posted August 4, 2013 Share Posted August 4, 2013 Hello, It looks like you're missing the opening and closing braces { } for the for loop. Also to note, the times variable, you can set it inside the for loop instead, like this: for(var times = 1; times <= 12; times++) Link to comment Share on other sites More sharing options...
davej Posted August 4, 2013 Share Posted August 4, 2013 (edited) Sorry, I thought it might be better without a for-loop for this. The tutorial for-loop is here... http://www.w3schools.com/js/tryit.asp?filename=tryjs_loop_for_ex You shouldn't begin any identifiers with a number, so id's or function names or variable names should start with a letter. Use something like f12 rather than 12f. <!DOCTYPE html><html><head><title>13</title><script>window.onload = init;var times;function init() {times = 1; out = document.getElementById('out');document.getElementById('btn').onclick = times13; }function times13(){ if (times <= 13){out.innerHTML += times + " times 13 is " + (times * 13) + "<br/>";times++;}else{times = 1;out.innerHTML = '';}} </script></head><body><h2> 13 Times Table </h2><input type="button" id="btn" value="13Times"/><br/><br/><div id="out"></div> </body></html> Edited August 4, 2013 by davej Link to comment Share on other sites More sharing options...
HumbleApprentice Posted August 12, 2013 Author Share Posted August 12, 2013 Thanks DaveJ. Your respond was very helpful. Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now