0

I want to have a jquery function that runs if the width of my div (.slider) is between 650px and 521px. I have this so far for code which isn't working and I am unsure of how to write the greater than less than part:

} else if ($(".slider").css("width") < "651px" || > "520px") {

I am getting this error in the console: Uncaught SyntaxError: Unexpected token >

*******UPDATE***** I am now using:

 var w = parseFloat($('.slider').css('width')) 

 if (w > 651) {
              //do something here
 } else if (w < 651 || w > 520) {
              //do something here
 } else if (w <= 520) {
             //do something here
 } else {
            //do something here
 } 

but for some reason it is stuck at the 'else if (w < 651 || w > 521)' conditional so when my div is less than 520 in width it is still firing what's inside that conditional.

3
  • expression wrong ? + cant compare strings as number values (num+'px')!! Commented Jul 1, 2017 at 22:49
  • I know the expression is wrong. Is there a solution to the "cant compare strings as number values (num+'px')" problem? Commented Jul 1, 2017 at 22:54
  • 1
    well, it's getting stuck because it will always be either smaller than 651 or bigger than 520, so that elseif will always return true. you need && not || Commented Jul 1, 2017 at 23:36

3 Answers 3

0

jsFiddle: https://jsfiddle.net/vjyoqdpv/

jQuery

$(function()
{
    var flexWidth = $('.flex').css('width').replace(/[^-\d\.]/g, '');
  if(flexWidth > 550 || flexWidth < 660)
  {
    alert("If statement is true");
  }
});

Using the jQuery.css this will return the value, however because it will return 550px this can't be used to check if greater or lesser than because it is a string. So you need to find convert it to a number (remove the px). Once you have the numeric value you can then use it correctly for your if statement.

Coverting css property to number URL: How to get just numeric part of CSS property with jQuery?

Sign up to request clarification or add additional context in comments.

4 Comments

This. is good except that flexWidth here is still a string. Although js would convert it during the comparison, parseInt or parseFloat might work better. var flexWidth = parseFloat($('.flex').css('width'))
You don't need the if (w > "651")
@Canvas sorry my copy & paste went bad. I am using: if (w > 651) { //do something here } else if (w < 651 || w > 520) { //do something here } else if (w <= 520) { //do something here } else { //do something here } but for some reason it is stuck at the 'else if (w < 651 || w > 521)' conditional so when my div is less than 520 in width it is still firing what's inside that conditional.
Change you || or statement to a && and statement
0

You need to add the variable to each side.

} else if ($(".slider").css("width") < "651px" || $(".slider").css("width") > "520px") {

Comments

0

The correct mode is: (another note: using multiple time $(".slider") slow up the performance)

 var w = $(".slider").width();
/**tons of code here**/
else if (w < 651 || w > 520) {
/** **/

3 Comments

This compares two strings though. Consider the case where "540px" < "525600px" returns false.
Not really! The jQuery width() function return a float not a string variable
My comment refers to a previous version of the answer that used .css('width')

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.