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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T01:42:34+00:00 2026-05-28T01:42:34+00:00

I have a simple function that sets the width of a bar based on

  • 0

I have a simple function that sets the width of a bar based on an argument.

And I call the function on .each with jQuery.

The console logs the statement correctly, showing me it seems to work. However, the style seems to be overridden by the last value found.

Here is the function:

function barGraph(innerWidth, barWidth) {
    innerWidth = parseInt(innerWidth) * .01 || .50;
    barWidth = parseInt(barWidth) || 267;

   // find percentage of total width
   var innerWidth = Math.floor(innerWidth * barWidth);

   var $innerBar = $('.slider-box div');

   $innerBar.css('width', innerWidth + 'px');

   console.log("Width should be: " + innerWidth + 'px');
}

then i call the function on each with jQuery:

$(document).ready(function() {
   var $innerBar = $('.slider-box div');

   $innerBar.each(function(index) {
       var newWidth = $(this).attr("data-bar-width");
       barGraph(newWidth, 267);
   });
});

the console log shows 10 times, with all appropriate widths. However, the style for all is the same as the last width.

Can someone help explain how I get the function to set the width of the currently selected div?

Thanks so much in advance,

Adam.

  • 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-28T01:42:35+00:00Added an answer on May 28, 2026 at 1:42 am

    Let’s break it down

    $(document).ready(function() {
       var $innerBar = $('.slider-box div');
    
       // going to call the barGraph function on each matching element
       // so far, so good
       $innerBar.each(function(index) {
           var newWidth = $(this).attr("data-bar-width");
           barGraph(newWidth, 267);
       });
    });
    

    Then in barGraph

    function barGraph(innerWidth, barWidth) {
        innerWidth = parseInt(innerWidth) * .01 || .50;
        barWidth = parseInt(barWidth) || 267;
    
       // find percentage of total width
       var innerWidth = Math.floor(innerWidth * barWidth);
    
       // getting all the matching elements (again)
       var $innerBar = $('.slider-box div');
    
       // setting the width of each matched element to 
       // the innerwidth calculated in this barGraph call.
       $innerBar.css('width', innerWidth + 'px');
    
       console.log("Width should be: " + innerWidth + 'px');
    }
    

    So, the barGraph function is run as many times as there are matched elements in $('.slider-box div'), but each run sets the width of all matched elements. In effect, the last run will set the width of all matched elements to whatever the innerWidth is calculated to be on the last run. Is that what you want to happen?

    What is more likely is perhaps something like this

    $(function() {
       var $innerBar = $('.slider-box div');
    
       // going to call the barGraph function on each matching element
       // so far, so good
       $innerBar.each(function(index) {
           var bar = $(this),
               newWidth = bar.attr("data-bar-width");
           barGraph(bar, newWidth, 267);
       });
    
       function barGraph(bar, innerWidth, barWidth) {
           innerWidth = parseInt(innerWidth, 10) * .01 || .50;
           barWidth = parseInt(barWidth, 10) || 267;
    
           innerWidth = Math.floor(innerWidth * barWidth);
    
           bar.css('width', innerWidth + 'px');
    
           console.log("Width should be: " + innerWidth + 'px');
       }
    
    });
    

    If the barGraph function is not used outside of the each call, then I might be inclined to move the function body inside of the anonymous function passed to each or modify barGraph function to be the function passed to each i.e.

    $(function() {
    
       $('.slider-box div').each(barGraph);
    
       function barGraph(index, element) {
           var bar = $(this),
               newWidth = bar.attr("data-bar-width");
    
           newWidth = parseInt(newWidth , 10) * .01 || .50;
           newWidth = Math.floor(innerWidth * 267);
    
           bar.css('width', newWidth + 'px');
    
           console.log("Width should be: " + newWidth + 'px');
       }
    
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a simple function that I want to call in the code behind
I have a function that accepts an anonymous function as an argument and sets
I have a simple function that returns an NSString after decoding it. I use
I have a very simple function that takes a list of comma separated (x,y)
I have a simple recursive function RCompare() that calls a more complex function Compare()
I have this simple code that speaks for itself. <script language='javascript"> function check() {}
I have a simple JavaScript file that has three jQuery $document.ready functions. All three
I have a simple menu on a web page, based on the jQuery Accordion.
Simple question here. I have to define classes that will contain sets of books,
I have a fairly simple python loop that calls a few functions, and writes

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.