0

Hello I have a jQuery function that I would like to work only between 1199px and 700px. Is this possible? I found the following code, it works so when it hits 1199px the function kicks in but how do I make it so at 700px the function stops working?

function checkWidth() {
    if (jQuery(window).width() < 1199) {
        //do something
    } else {

    }
};
checkWidth();

Thanks!

cup_of
  • 6,397
  • 9
  • 47
  • 94
  • Have you tried adding `&& jQuery(window).width() > 700` to your if statement? – cakan Aug 19 '16 at 06:35
  • Just to ask, what happens if I resize my window? Or when I start with a minimized window on desktop and than maximise it to fullscreen? I'm just asking, because I don't see a good usecase for the thing you want to do! – eisbehr Aug 19 '16 at 06:40
  • @eisbehr this is to target mobile devices, i started at 1199px to cover every tablet basically and 700px to cover basically every mobile device. laptops generally dont go smaller than 1199px. so basically if my page is loaded only on tablet the function will work. u are right that if you start at 1500px and shrink down to 1199px it will not work, but i am not really concerned with that. – cup_of Aug 19 '16 at 06:51

5 Answers5

2
function checkWidth() {
    if (jQuery(window).width() < 1199 && jQuery(window).width() > 700) {
        //do something
    } else {

    }
};
checkWidth();

Also:

I would store the width as a variable so you don't have to call that twice.

function checkWidth() {
    var windowWidth = jQuery(window).width();
    if (windowWidth < 1199 && windowWidth > 700) {
        //do something
    } else {

    }
};
checkWidth();
Derek Story
  • 9,518
  • 1
  • 24
  • 34
1

Try this:

function checkWidth() {
    var width = jQuery(window).width();
    if (width >= 700 && width < 1199) {
        /* do something */
    } 
};
checkWidth();
jedifans
  • 2,287
  • 1
  • 13
  • 9
0

You can combine the condition using and logical operator like this:

if (jQuery(window).width() < 1199 && jQuery(window).width() > 699) {
Bhojendra Rauniyar
  • 83,432
  • 35
  • 168
  • 231
  • this worked as well, thanks! unfortunately i can only accept one answer. I gave you an upvote though – cup_of Aug 19 '16 at 06:45
0

Rather than check inside function , check it before calling function

 function checkWidth() {

            /* do something */

        } 


    var width=jQuery(window).width();
    if(width<1199 && width>700){
    checkWidth();
    }
Jekin Kalariya
  • 3,475
  • 2
  • 20
  • 32
0

You than need to add a second condition to the if. It could than look like this:

if ($(window).width() < 1199 &&
    $(window).width() >= 700)
Morris Janatzek
  • 572
  • 3
  • 11