Search code examples
javascriptjqueryhtmlscreen-resolution

Detecting and changing via screen width is not working


So, I checked out Do something if screen width is less than 960 px to see how to execute something if the screen size was less than 960 px. However, it did not work for me. Their answer was this:

if ($(window).width() < 960) {
   alert('Less than 960');
}
else {
   alert('More than 960');
}

I tried it and it did work. I also already had an alert that told me the screen size;

var size = $(window).width();
alert(size);

and this worked, but this did not:

if (size < 1200) {
    $("#mobileVersionDetected").css("display", "block");
}
else {
    $("#mobileVersionDetected").css("display", "none");
}

Any ideas?


Solution

  • If I add $(window).width() into the actual if function rather than using a variable it works.