Search code examples
jqueryif-statementmargin-left

how to use ' > ' or ' < ' in if condition jquery with margin left?


ex :

 if ( $div.css('marginLeft') == '-2000px') {

 } ..... it is working but i want to check it like

 if ( $div.css('marginLeft') > '-2000px') {

 } ...... Not working

Solution

  • The problem is that '-2000px' is a string, and not a number. You cannot use > and < on strings in this way.

    Try parsing $div.css as an integer, and writing the pixels as an integer (without the ''):

    if ( parseInt( $div.css('marginLeft') ) > -2000 ){