Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JQuery/Javascript and the use of && operators

I'm trying to get a simple conditional statement to work, and running into problems. The failing code:

    $(document).ready(function(){
    var wwidth = $(window).width();

    if (wwidth < 321) {
        alert("I am 320 pixels wide, or less");
        window.scrollTo(0,0);
    } else if (wwidth > 321) && (wwidth < 481) {
        alert("I am between 320 and 480 pixels wide")
    }
});

If I remove the else if part of the code, I get the alert. If I try to use && or || operators it will fail. I've Googled, I can't find a reason why it's not working. I've also tried:

((wwidth > 321 && wwidth < 481))

along with other ways, just in case it's some odd syntax thing.

Any help would be greatly appreciated. Thanks :)

like image 469
mrEmpty Avatar asked Apr 21 '12 17:04

mrEmpty


2 Answers

((wwidth > 321) && (wwidth < 481))

This is the condition you need (http://jsfiddle.net/malet/wLrpt/).

I would also consider making your conditions clearer like so:

if (wwidth <= 320) {
    alert("I am 320 pixels wide, or less");
    window.scrollTo(0,0);
} else if ((wwidth > 320) && (wwidth <= 480)) {
    alert("I am between 320 and 480 pixels wide")
}
like image 101
Mikey Avatar answered Sep 21 '22 14:09

Mikey


if (wwidth > 321 && wwidth < 481) {
//do something
}
like image 31
Jonny Burger Avatar answered Sep 20 '22 14:09

Jonny Burger