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 6564157
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T13:55:54+00:00 2026-05-25T13:55:54+00:00

Im trying to write a function that resizes the css widths of some elements

  • 0

Im trying to write a function that resizes the css widths of some elements when the browser window is resized, right now I dont seem to be getting things right and would love it if someone could help me out on where Im going wrong, my code so far is:

function windowResize() {
            $('#content').css('width', $windowWidth);
            $('#div.mySection').css('width', $windowWidth);
            $('#div.mySection .story').css('width', $windowWidth);
        }

$(document).ready(function() {

            var $windowWidth = $(window).width();               

            $(window).resize(function() {
                windowResize();
            });

        });

Im not sure if I have placed everything in its correct place, if someone could advise me on how I can make this code much better it would be great.

Thanks
Kyle

  • 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-25T13:55:55+00:00Added an answer on May 25, 2026 at 1:55 pm

    You’ll want to recalculate window‘s new dimensions inside of the function you’re running when the window resizes; otherwise, you will wind up with the same width being repeated forever. (Incidentally, your $windowWidth variable is outside of the scope of your windowResize function, which is why it is not being used now.)

    function windowResize() {
            var windowWidth = $(window).width(); 
            $('#content').css('width', windowWidth);
            $('#div.mySection').css('width', windowWidth);
            $('#div.mySection .story').css('width', windowWidth);
        }
    
    $(document).ready(function() {
    
            var $windowWidth = $(window).width();               
    
            $(window).resize(function() {
                windowResize();
            });
    
        });
    

    By convention, variables storing jQuery objects are prefixed with $, so I removed that from windowWidth, which is a plain old Number.

    Now, here’s the caveat for your code: resize fires continuously in certain browsers, which means you are going to be running your handler continuously, too, which could be a very bad thing. Give Ben Alman’s throttle/debounce plugin a try; it will limit the number of times your handler is called, “debouncing” or “throttling” execution to once for a given interval. That way, you’ll achieve your effect without spamming resizes to your DOM elements (which can be painful and is a good way of locking a UI).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to write a function that will produce the combinations of list elements
I'm trying to write a function that formats every (string) member/variable in an object,
I am trying to write a function that will pull the name of a
I'm trying to write a function that is able to determine whether a string
I'm trying to write a regex function that will identify and replace a single
I am trying to write a JavaScript function that will return its first argument(function)
I'm trying to write a custom function that will let me retrieve a cell
I am trying to write my first real python function that does something real.
Im trying to write a function that takes my decimal degrees (lat or long)
I am trying to write a function that takes two matrices as parameters and

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.