Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

103
Views
Get one jQuery media query to fire

I have three CSS @media queries and I need to run some JavaScript to measure the height of a div and move a second div on some screen sizes. The issue is that all of my queries are applying, obviously because they all return true. How can I adjust this so that only one of the conditionals fire in jQuery, similarly to CSS?

//Responsive Adjustments
jQuery(document).ready(function($) {
  $(window).resize(function () {
    console.log($(window).width());
    
    if ($(window).width() <= 1024) {
      console.log ('tab land');
    }

    if ($(window).width() <= 980) {
      console.log ('tab port');
    }

    if ($(window).width() <= 479) {
      console.log ('phone port');
    }
  });
});
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

Use else if and check from mobile upwards:

//Responsive Adjustments
jQuery(document).ready(function($) {
  $(window).resize(function() {
    console.log($(window).width());

    if ($(window).width() <= 479) {
      console.log('phone port');
    } else if ($(window).width() <= 980) {
      console.log('tab port');
    } else if ($(window).width() <= 1024) {
      console.log('tab land');
    }
  });
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

about 4 years ago · Juan Pablo Isaza Report

0

You could adjust your conditions:

//Responsive Adjustments
jQuery(document).ready(function($) {
  $(window).resize(function() {
    console.log($(window).width());

    if ($(window).width() <= 1024 && $(window).width() > 980) {
      console.log('tab land');
    }

    if ($(window).width() <= 980 && $(window).width() > 479) {
      console.log('tab port');
    }

    if ($(window).width() <= 479) {
      console.log('phone port');
    }
  });
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!