0

I have a project on jsFiddle in which I want to link a button to the following Javascript code:

var computerChoice = math.random();

if (computerChoice < 0,5) {
    console.log("you lost")
} else {
    console.log("you've won"
}

So if I press the button, a random number should be generated leading to one of the two responses. How can I get this working?

5
  • 4
    Please read the jQuery tutorial about basic event handling. That's what tutorials are there for, to teach you the basics of a library/framework/etc. Stack Overflow is not the right place to learn these basics. To learn more about event handling in general, I recommend the quirksmode.org articles. Commented Mar 1, 2014 at 18:14
  • 1
    Capitalize the m in Math! Commented Mar 1, 2014 at 18:23
  • @Charlie given the question originally asked for, and the jsFiddle currently contains jQuery, I'd suggest reverting the edit you made. Commented Mar 1, 2014 at 18:31
  • Awesome DA, thanks a lot. One more thingie, what would be statement if I want to push a msgbox in stead of console.log (depending on the random numer, so fe, a msgbox with : you've lost when the number is <0,5 Commented Mar 1, 2014 at 18:33
  • 1
    @DA. Now that it's tagged correctly, there's no need to include the text "I have a question about jquery." Commented Mar 1, 2014 at 18:41

5 Answers 5

1
$('#yourButton').click(function(){
    // insert your JS here
})

To elaborate...the above is attaching a click event listener to your button. This is the very essence of jQuery: select an item from the DOM, attach logic to it. The above assumes you gave your button an id of 'yourButton' but you could select the button in any number of ways using jQuery selectors (google that to find plenty of tutorials).

Sign up to request clarification or add additional context in comments.

Comments

1
function tosser(){
    if (Math.random() < 0.5) {
        return "you lost";
    } else {
        return "you\'ve won";
    }
}
$("button").on("click",function(){
    alert(tosser());
});

Comments

0

I have a question about jquery.

I'm assuming you can use jQuery then. Call the function on the click event:

function randomNumber(){
    var computerChoice = Math.random();
    if (computerChoice < 0.5) {
        console.log("you lost")
    } else {
        console.log("you've won");
    }
}

$("button").click(randomNumber);

Or:

var button = document.getElementById("button");
button.onclick = function(){
    randomNumber();
}

Example

Also, you need to capitalize the m in Math.

1 Comment

@null He asks about jQuery in the first line, and he includes it in his fiddle.
0

fiddle

Pass your function name in onclick event of your button

<button onclick="randomFunction();">click to call function randomFunction</button>

Script

function randomFunction() {
//here should be your code     
}

2 Comments

Inline javascript is so bleh.
Note the question is likely looking for a more jQuery-esque solution than inline JS calls.
0

This might help you:

  • Math.random() not math it's case sensitive
  • $('#yourButton').click(function(){}) from DA. (This is useful)
  • onclick="www.nu.nl" will not work if you want you can add
  • 0.5 is a number in JS not 0,5
  • There is soo small chance to win because 0.6187859538476914 :D number = number.toFixed(1);
  • 1 represent decimal places
  • <Br> doesnt need to be closed
  • And something a lot useful is F12 in Chrome and Console tab

user181796 have a nice rest of day!

JS

$('#Start').click(function(){
    number = Math.random();
    number = number.toFixed(1);
    if (number < 0.5) {
        console.log("you lost " + number);
    } else {
        console.log("you win " + number);
    }
});

Head

    <script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>

Html

<span> text box 1 </span>
 <input class = "styleYellow" type = "text"> </input> 
 <br> <br>
<span> text box 1 </span><input type = "text"> </input> 
 <br> </br>    

<button id="Start">Ga naar pagina 1</button>
<button id="I_dont_know">Ga naar pagina 2</button>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.