user181796
user181796

Reputation: 195

Link html button to javascript

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?

Upvotes: 0

Views: 103

Answers (5)

VeeeneX
VeeeneX

Reputation: 1578

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>

Upvotes: 0

Siddique Mahsud
Siddique Mahsud

Reputation: 1453

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     
}

Upvotes: 0

Moob
Moob

Reputation: 16184

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

Upvotes: 1

Charlie
Charlie

Reputation: 11777

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.

Upvotes: 0

DA.
DA.

Reputation: 40671

$('#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).

Upvotes: 1

Related Questions