Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3805388
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T14:40:12+00:00 2026-05-19T14:40:12+00:00

Hello friends I am trying to add a class to body dynamically depending on

  • 0

Hello friends I am trying to add a class to body dynamically depending on the browser window resolution. Here is the code I am trying to use but need help tuning it as I dont know jQuery at all.

The options I want to achieve are :

Once a visitor comes to my site, this code must check his browser windows size and add class to body as per the following rules

  1. If window size is more than 1024px but less than 1280px then add class .w1280.

  2. If window size is more than 1280px but less than 1440px then add class .w1440.

  3. If window size is more than 1440px but less than 1280px then add class .w1680.

  4. If window size is more than 1680px then add class .wLarge.

To achieve this, I am trying to use the following script. My questions are:

  1. Is this the correct code? If not what is the correct code?

  2. Is this the best shortest possible code? If not what will be the correct code?

Kindly help as my knowledge of jQuery is almost ZERO.

function checkWindowSize() {  
    if ( $(window).width() > 1024) { 
        $('body').addClass('w1280');  
        } else {  
        $('body').removeClass('w1280');  
    } 
    if ( $(window).width() > 1280 ) { 
        $('body').addClass('w1440');  
        } else {  
        $('body').removeClass('w1440');  
    } 
    if ( $(window).width() > 1440) { 
        $('body').addClass('w1680');  
        } else {  
        $('body').removeClass('w1680');  
    } 
    if ( $(window).width() > 1600) { 
        $('body').addClass('wLarge');  
        } else {  
        $('body').removeClass('wLarge');  
    } 
}    
checkWindowSize()
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-19T14:40:13+00:00Added an answer on May 19, 2026 at 2:40 pm

    If you aren’t storing any other classes on the body element, you could do this:

    function checkWindowSize() {
        var width = $(window).width();
        document.body.className = width > 1600 ? 'wLarge' :
                                  width > 1440 ? 'w1680' :
                                  width > 1280 ? 'w1440' :
                                  width > 1024 ? 'w1280' : '';
    }
    

    Some people might advise you to do it with a switch statement, but then, some people also like to eat their young.

    This function will overwrite the body‘s class every time it’s called (the default, if the browser is smaller than/equal to 1024 pixels, is no class at all), so like I said it won’t work if your body has other classes that need to be maintained.

    EDIT Per Šime’s suggestions, here’s a safer way to do it:

    function checkWindowSize() {
        var width = $(window).width(),
        new_class = width > 1600 ? 'wLarge' :
                    width > 1440 ? 'w1680' :
                    width > 1280 ? 'w1440' :
                    width > 1024 ? 'w1280' : '';
    
        $(document.body).removeClass('wLarge w1680 w1440 w1280').addClass(new_class);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hello Functional C# Friends, So this time i am trying to compact my code
Hello friends I have three div tags on my page.. I am trying to
I'm trying to run the following code but unfortunately facing Error problems package jskypeexample;
Hello friends i am to add optional parameter to pass these in a method
Hello can anybody solve this please I'm creating the object in the action class
Hello I am compiling a program with make but I get the error of
Hello I am having trouble using multiquery in facebook .net. I am trying to
For a present, I am trying to create a code that reads as a
I use to work with the animation on jquery but never ajax, so it's
I'm trying to create more useful debug messages for my class where store data.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.