cfocket
cfocket

Reputation: 109

If statement wrong syntax jQuery


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

Answers (4)

V31
V31

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

Merlin
Merlin

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

Muhammad Saud
Muhammad Saud

Reputation: 354

remove ; at the end of if statement.

if ($(window).width() < 900);

to

if ($(window).width() < 900)

Upvotes: 1

Felix
Felix

Reputation: 38102

Remove ; after your if statement:

if ($(window).width() < 900)

Upvotes: 5

Related Questions