1

I have an if statement on my website which does commands when the page is under a certain size. there is one small problem. if the page size is over 1000 it automatically says the if statement is not true and goes to the else. my attempts are bellow

if($("body").css("width") < 700 + "px")

I have tried a plain < and I have also done <= I'm kinda new to jquery, all help would be kindly appreciated.

2 Answers2

1

Better way of doing it with jQuery is using width function instead of css.

Something like this:

if ($(window).width() < 1000) { 
    alert('Less than 1000'); 
} else { 
    alert('More than 1000'); 
}
Jakob
  • 3,493
  • 4
  • 26
  • 42
0

You are comparing strings. Strip off the trailing "px" from the left value (How to delete "px" from 245px) and compare numbers.

if(parseInt($("body").css("width") , 10) < 700)
Community
  • 1
  • 1
Robert Columbia
  • 6,313
  • 15
  • 32
  • 40