[ACCEPTED]-jQuery Window Width else if statement-width

Accepted answer
Score: 18

You are missing a couple >= in your code, and 3 windowSize is not being compared but assigned 2 a new value as a result of statements like 1 windowSize = 480. Try this version instead:

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

        if (windowSize <= 479) {
            console.log("screen width is less than 480");
        }
        else if (windowSize <= 719) {
            console.log("screen width is less than 720 but greater than or equal to 480");
        }
        else if (windowSize <= 959) {
            console.log("screen width is less than 960 but greater than or equal to 720");
        }
        else if (windowSize >= 960) {
            console.log("screen width is greater than or equal to 960");
        }
    }

    // Execute on load
    checkWidth();
    // Bind event listener
    $(window).resize(checkWidth);
});​
Score: 2

You're missing a greater than sign :

else if (windowSize = 720

and 1 using just the equal sign ?

Try this instead:

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

        if (windowSize < 480) {
            console.log("screen width is less than 480");
        }
        else if (windowSize < 720) {
            console.log("screen width is less than 720 but greater than or equal to 480");
        }
        else if (windowSize < 960) {
            console.log("screen width is less than 960 but greater than or equal to 720");
        }
        else {
            console.log("screen width is greater than or equal to 960");
        }
    }

    // Execute on load
    checkWidth();
    // Bind event listener
    $(window).resize(checkWidth);
});​

FIDDLE

Score: 2

It's because of your else if statements. You're 3 checking with a single equal sign, which 2 is assigning the value.

if ( windowSize = 480 && windowSize <= 719 )

when you should be 1 doing

if ( windowSize == 480 && windowSize <= 719 )

or >=, if that's the intended logic.

More Related questions