Note that there are some explanatory texts on larger screens.

plurals
  1. POSimple Button Test with Javascript Function
    primarykey
    data
    text
    <p>I am just finishing up learning JavaScript on Codecademy.com and I was trying a simple function out with an html button. My problem is that I guess I don't understand how the JS I learned equates to usable code and I've searched these forums and tried the document.getElementById.onclick method and I tried using onClick="adventure();" in the button itself and I cannot get anything to work. Please explain to me how you apply JS to buttons so that it works in a browser. Thank you.</p> <pre><code>&lt;!doctype html&gt; &lt;html&gt; &lt;head&gt; &lt;script type="text/javascript"&gt; document.getElementById("adv").onclick = adventure(); function adventure() { if (confirm('I am ready to play!')) { var age = prompt("What's your age?"); if(age &lt; 18) { console.log('I cannot stop you from playing, but do so at your own risk!'); } else { console.log('You have a great challenge, ahead. Brace yourself.' + "\r\n"); } console.log("You are walking down the street and you see Shawn beating up your friends and stealing their candy. You feel as though it is your duty to respond. You walk up to him and say, 'Hey, Shawn, knock it off!" + "\r\n"); console.log("Shawn glares at you." + "\r\n"); var choice1 = function() { var userAnswer = prompt("Do you wish to fight?" + "\r\n"); if(userAnswer !== "yes" || userAnswer !== "no") { console.log("You must choose 'yes' or 'no'."); choice1(); } else if (userAnswer === "yes") { console.log("As you go to kick Shawn, he grabs your leg and slams you to the pavement. As the world turns black and your eyes slowly close, you feel the end is near. You cry, and then perish. Goodbye!"); } else { console.log("Good choice, weakling. By playing the coward you will survive. Now where's my beer?"); } }; } &lt;/script&gt; &lt;/head&gt; &lt;body&gt; &lt;input id="adv" type="button" onclick="adventure();" value="Start Your Adventure" /&gt; &lt;/body&gt; &lt;/html&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload