Using jQuery's addClass() method and then trying to access the width property produces an inaccurate

In my project, I am encountering inconsistent return values for width when adding an "active" class to a selection of 4 items. These items are part of a horizontal slider that needs to adjust its elements' widths as one of them becomes active and changes due to a different font weight being applied.

The issue arises when I add the "active" class and immediately try to obtain the width of the element; it returns the old value instead of the updated one. To test this, I changed the class, logged all widths, then introduced a timeout of 50ms before logging the widths again right after. The results clearly show a discrepancy in the values:

pageNavButtons.addClass("active");
pageNavButtons.each(function () {
    console.log(Math.ceil($(this).get(0).getBoundingClientRect().width));
});

$timeout(function(){
    pageNavButtons.each(function ({
        console.log(Math.ceil($(this).get(0).getBoundingClientRect().width));
    });
},50);

Expectedly, I should see identical widths logged both times. However, what actually happens is as follows:

207
217
196
176

207
220
200
179

This inconsistency persists consistently. It seems like the renderer is still processing, leading me to wonder how I can make it wait. Attempting window.requestAnimationFrame did not solve the issue, and resorting to a timer would be considered a less optimal solution.

It's worth noting that I opted for getBoundingClientRect over jQuery's outerWidth() due to the latter's rounding down behavior, necessitating sub-pixel precision.

Any insights or suggestions on how to address this challenge?

Answer №1

I'm not sure I understand your question :

$timeout(function(){....

perhaps you're looking for something like this:

setTimeout(function(){
    pageNavButtons.each(function ({
     console.log(Math.ceil($(this).get(0).getBoundingClientRect().width));
});

}, 2000);

This code snippet will delay the execution of the inner script;

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

The information in the table is spilling out beyond the boundaries

I'm facing an issue with my bootstrap table where if a field value is too long, the table extends past the container length. I tried using responsive-table to prevent this, but then a scroll bar appears at the bottom, making it difficult to view the d ...

New to CSS/Ruby - What causes two adjacent buttons to have varying sizes?

The varying sizes of my search and login buttons located beside each other are puzzling me. Could it be due to the fact that the search button is enclosed within a submit_tag argument while the login button is not? If this is indeed the case, how can I mak ...

Embracing Divs Similar to the Grid Selector in Windows Phone

Can someone help me create square divs in HTML 5, similar to the example shown? I have written this HTML5 Code - what should I include in the CSS file to achieve the desired outcome? (If Bootstrap can be used, please provide the necessary classes instead ...

What is the best way to incorporate alternating classes into a dynamic feed of thumbnail images?

I am integrating a YouTube user's video channel feed onto a webpage using two plugins called jYoutube and jGFeed. If you are interested in the plugins I mentioned, here they are: jGFeed: jYoutube: However, I have encountered an issue with the im ...

Tips on displaying hyperlinks within a text area in an Angular application

In my Angular application, I am facing an issue with displaying hyperlinks within a text area box for dynamic content. The hyperlinks are not recognized and therefore cannot be clicked. Can someone please advise on how to properly display hyperlinks with ...

Triggering a JQuery event to switch between tabs

Currently, I am in the process of developing a web application that incorporates nav-tabs from Bootstrap. These nav-tabs consist of 4 different tab-contents. My main concern lies in determining which nav-tab the user has selected among the 4 tabs and updat ...

Footer refuses to stay anchored at the bottom of the page

I'm struggling to keep my footer at the bottom of all pages on my blog. I am facing two main issues. If I use position:absolute, the footer ends up in the middle of the main blog page. Alternatively, when I don't use it, the footer sticks to the ...

Integrating JSON Data into Highcharts Visualizations

I am facing an issue with High Charts as I am trying to display a chart using details from a JSON data example. [{"eventName":"ABC","countError":147391},"eventName":"DEF","countError":117926}] The JSON code only contains 2 parameters - eventName and coun ...

choose the most recently selected item from a selection to attribute a specific characteristic

I have a list of video icons where each icon toggles a popup div on click. I want the latest popup to always appear on top of the previously shown popups. Below is the jQuery code I am using: $('.icon_videonew').each(function () { // ale ...

Error Encountered when Using JQuery AJAX: Unexpected Identifier Syntax Issue

I've been struggling with a strange error for quite some time now. I want to believe that this is one of those errors where the solution will magically appear, but only time will tell. Here's the piece of code causing the issue: var images = ...

AJAX request bypassed form validation

My form utilizes an AJAX call to submit information to Google Sheets, which works well until I try to incorporate form validation. At that point, only the AJAX call seems to run. Below is the HTML Form: <!DOCTYPE html> <html class="no-js" lang=" ...

overflow with a height of 100%

I want to add vertical scroll bars to my left and right columns without setting the height to 100%. How can I achieve this scrolling effect without specifying height: 100%? html,body{height:100%;} .row > .sidebar-fixed { position: relative; to ...

Ways to transition between divs using clickable buttons

I have a coding challenge where I need to implement two panels. The lower panel consists of three buttons and upon clicking each button, different data should load in the upper panel. For example, when button 1 is clicked, divs 1.1, 1.2, and 1.3 should sl ...

Tips for changing the appearance of blockquote lines in bootstrap 5

I'm trying to see if it's possible to customize the blockquote-footer line in bootstrap 5. Can I change its color and width? Here's the code snippet from bootstrap 5: <figure> <blockquote className="bloc ...

Reiterating the text and determining the length of the string

Currently, the script is capable of calculating the width of the script based on user input and can also determine the width of a string. However, I am facing an issue when attempting to repeat a string entered by the user. For example, if the user input ...

JavaScript: Various Outputs Depending on Input

There seems to be a discrepancy in the results when I input "coup," "cou," and then "coup." Can anyone offer insight into why this might be happening? To view my code on jsfiddle, click here: jsfiddle.net/xbuppd1r/43 var data = {"cards":[{"$type":"Assets ...

Using CSS backdrop-filter to create a unique blurred border effect for your web elements

In my quest for achieving unique visual effects, I have been experimenting with the backdrop-filter and element borders. While it is easy to create an element that blurs its background, I am facing challenges in making the filter affect only the border are ...

The callback function is not being executed in the Ajax request

$(document).ready(function(){ var requestURL = 'http://www.football-data.org/soccerseasons?callback=?'; $.ajax({ type: 'GET', dataType: 'json', url: requestURL, success: function(data){ cons ...

BxSlider Firefox Links Unresponsive: Troubleshooting Tips

While everything works fine on Chrome, I'm facing an issue where I can't click on links that are placed over images on Firefox. For example: Does anyone have any suggestions or ideas on how to solve this problem? Thank you in advance! ...

Button to save with jQuery Ajax and a button to save and exit

Seeking guidance on jquery for creating a simple form with Save and Save-Exit buttons. Is there a way to achieve this? I currently have a setup that is partially working. After submitting, if I click save-exit, then return to /messages, the form still pos ...