I'm trying to create a random number in Jquery I'm not sure if I need to store it to do the next part maybe you guys could help me out on that first off my random number generator;
$( "#arrowleftup" ).click(function() {
var number = 1 + Math.floor(Math.random() * 10);
$('#storenumber').text(number);
});
I want to generate a random number when my Img with the id "arrowleftup" is clicked, I then want to pass my number into this -
If ('#storenumber' == 8=>){
$('.battle').show();
} else {
die
};
So that if the number was 8 or higher it would show a hidden div, can anyone spot the error in my code? Also do I need to store the value to pass it into the if statement
JSFiddle - /
thanks!
I'm trying to create a random number in Jquery I'm not sure if I need to store it to do the next part maybe you guys could help me out on that first off my random number generator;
$( "#arrowleftup" ).click(function() {
var number = 1 + Math.floor(Math.random() * 10);
$('#storenumber').text(number);
});
I want to generate a random number when my Img with the id "arrowleftup" is clicked, I then want to pass my number into this -
If ('#storenumber' == 8=>){
$('.battle').show();
} else {
die
};
So that if the number was 8 or higher it would show a hidden div, can anyone spot the error in my code? Also do I need to store the value to pass it into the if statement
JSFiddle - http://jsfiddle/7t59E/
thanks!
Share Improve this question asked Aug 25, 2013 at 18:14 user2598957user2598957 2636 silver badges12 bronze badges 7- 1 There are so many errors in this small code that we should probably only remend a tutorial. – Denys Séguret Commented Aug 25, 2013 at 18:15
- Go for it I'm willing to learn, I know the die thing is from php I didn't know what to put there to help you understand what I was trying to do. – user2598957 Commented Aug 25, 2013 at 18:17
-
If
: Should beif
.'#storenumber' == 8
: Why are you paring a string against a number? It would never be true.8=>
: ??die
: doesn't exist in JavaScript. – Felix Kling Commented Aug 25, 2013 at 18:17 -
Start by using your browser debugging
console
– Brewal Commented Aug 25, 2013 at 18:17 - @Felix Kling, I was trying to say if the number is equal (==) to 8 or more (=>), then run. – user2598957 Commented Aug 25, 2013 at 18:19
4 Answers
Reset to default 4DEMO
Try this,
$(document).ready(function () {
$("#battle").hide();
$("#arrowleftup").click(function () {
var number = 1 + Math.floor(Math.random() * 10);
$('#storenumber').text(number);
if (number >= 8) $("#battle").show();
else $("#battle").hide();
});
});
You need to put the code in the event handler to execute it whenever a number is generated. There you also can just access the number
variable instead of getting it from the element text:
$( "#arrowleftup" ).click(function() {
var number = 1 + Math.floor(Math.random() * 10);
$('#storenumber').text(number);
if (number == 8) {
$('.battle').show();
} else {
// do nothing
}
});
(updated jsfiddle, also including a .battle
element and loading the jQuery library)
no need to store result into a dom element
you can do it via
$( "#arrowleftup" ).click(function() {
var number = 1 + Math.floor(Math.random() * 10);
$('.battle').hide();
if (number >= 8){
$('.battle').show();
}
});
U can try something like this ?
$( "#arrowleftup" ).click(function() {
$('#storenumber').text(parseInt(Math.random()*10));
if (parseInt($('#storenumber').text()) > 4) {
$("#toggleme").show();
} else {
$("#toggleme").hide();
}
});
Demo