Reputation: 109
I'm trying to implement a simple if/else statement in JavaScript but it's not working.
Can you tell me what is wrong with this syntax?
<script>
$(document).ready(function() {
if ($(window).width() < 900);
$(".stats").css("background-color", "red");
else
$(".stats").css("background-color", "white");
});
</script>
Upvotes: 0
Views: 89
Reputation: 7666
You can minimize the statements with
<script>
$(document).ready(function() {
var color = $(window).width() < 900 ? "red" : "white";
$(".stats").css("background-color", color);
});
</script>
Fiddler for the same http://jsfiddle.net/b8zLw/
Upvotes: 0
Reputation: 4917
Remove ; at the end of if condition
if ($(window).width() < 900)// here
if you want to change background color when windows is resizing use :
$( window ).resize(function() {
if ($(window).width() < 900)
$(".stats").css("background-color", "red");
else
$(".stats").css("background-color", "white");
});
EXAMPLE (use center column to resize)
Upvotes: 2
Reputation: 354
remove ;
at the end of if statement.
if ($(window).width() < 900);
to
if ($(window).width() < 900)
Upvotes: 1