jquery - Want to run only one if Condition -




what if screen width < 1200 alert('1200') alert shown , when screen width < 640 alert('640') alert shown. need without use of else block.

fiddle

if ($(window).width() < 1200) {    alert('1200');  }  if ($(window).width() < 640) {    alert('640');  }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

use if/else if. way first matching branch followed. note logic work in case you'd need reverse order of conditions. try this:

if ($(window).width() < 640) {    alert('640');  } else if ($(window).width() < 1200) {    alert('1200');  }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

as side note, detecting browser width sign you're amending ui different resolutions. if that's case can achieve same thing in much better way using css media queries.





wiki

Comments