jQuery's promise method in CSS is executed successfully

Seeking advice from someone:

Scenario: Currently, I have

<div id="breadCrumb" style="width:80%"></div>

Now, I want to update the width to 100% and retrieve the width in pixels on click

$('#menu_toggle').on('click', function() {
    var cache = $('#breadCrumb');

    cache.css('width', "100%").promise().done(function(){   
        console.log(cache.width());
    });
});

However, I am still getting the 80% value.

Only after clicking again do I receive the 100% value in pixels

The concept was to wait until the CSS is applied before retrieving the new width

Any thoughts or suggestions?

Appreciate your assistance!

Answer №1

css works quickly and efficiently.

cache.css('width', "100%");
console.log("breadCrumb width: " + cache.width());

This should function properly on Chrome and Firefox, unless there is an unseen issue:

$('#menu_toggle').on('click', function() {
    var cache = $('#breadCrumb');

    cache.css('width', "100%");
    console.log("breadCrumb width: " + cache.width());
    console.log("body width: " + $(document.body).width());
});
#breadCrumb {
  border-bottom: 2px solid blue;
}
<div id="breadCrumb" style="width:80%"></div>
<input id="menu_toggle" type="button" value="Click Me">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

If you require the browser to delay applying the change, you can utilize setTimeout:

$('#menu_toggle').on('click', function() {
    var cache = $('#breadCrumb');

    cache.css('width', "100%");
    setTimeout(function() {
      console.log("breadCrumb width: " + cache.width());
      console.log("body width: " + $(document.body).width());
    }, 50);
});
#breadCrumb {
  border-bottom: 2px solid blue;
}
<div id="breadCrumb" style="width:80%"></div>
<input id="menu_toggle" type="button" value="Click Me">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

Answer №2

It is recommended to utilize the setTimeout() function in order to apply your CSS and then incorporate it into your code like so:

$(document).ready(function(){
    var cache = $('#breadCrumb');
    alert(cache.width());
    cache.css('width', "100%")
    setTimeout(function(){
        alert(cache.width());
    },500)

});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="breadCrumb" style="width:80%"></div>

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

Styling Multiple Fullscreen Rows with Bootstrap CSS

Does anyone have any suggestions for the CSS code needed to stack five rows on top of each other? Each row should be 100% height and width of the browser. Here is the HTML code I currently have: <div id="wrapper"> <div class="container-fluid"> ...

Sorting a select element using Javascript/JQuery while taking into consideration the label attribute present in the options

It came to my attention that the jQuery options and examples only covered the text and value attributes, causing errors when attempting to add the label attribute. I am looking for a way to include the label attribute in the sorting process, especially whe ...

What is the method for determining the intersection of multiple rgba values?

If I have the following CSS code: .example { background-color: rgba(0, 0, 0, 0.5); border-color: rgba(0, 0, 0, 0.5); } Even though the background-color and border-color share the same rgba value, they appear as different colors due to how the bor ...

The Stepper StepIconComponent prop in MUI is experiencing issues when trying to render styles from the styles object, leading to crashes in the app

Struggling to find a way to apply the styles for the stepper component without using inline styles. I attempted to replicate Material UI's demo, but encountered an error. The code from Material UI's demo that I want to mimic is shown below: http ...

Comparison between triggering a 'click' event with or without indexing is often debated in web development circles. While trigger('click') invokes the click event

Can someone clarify the difference between using trigger('click') and trigger('click')[0]? Are there any distinctions between the two? ...

Preserving scroll location following a hyperlink postback situation

I've explored numerous strategies to tackle my issue, but none have proven successful. My task involves toggling user activation and deactivation through an asp.net hyperlink. The main problem arises when this action triggers a postback, causing the p ...

What is the best way to display an international phone number using Angular Material V6?

I've been working on a project that utilizes Angular Material V6. My goal is to display international phone numbers with flags in a Material component text box. After some research online, I came across an npm module that achieved this but it was tail ...

The same HTML/CSS appears with varying appearances

I have encountered an issue where I created two identical pages, with one page calling the other through a link. Surprisingly, my top menubar changes significantly between the two pages, even though the HTML/CSS code is exactly the same. <html> < ...

Display a PDF document within a div using jQuery or AngularJS

How can I display a pdf file inside a div using jQuery/AngularJs? For example: $.get('/helper/test.pdf', function(data){ $("#div1").html(data); // want to show the pdf in this div }); Or with AngularJs: $http({ meth ...

Track and maintain the active status of the clicked article using jQuery

I'm encountering an issue with jQuery where the parent ul li does not open as expected. Below is the structure of my HTML: <div> <ul> <li class="has-sub"> <a href="#">1</a> <ul> &l ...

JQuery requests functioning flawlessly on one system while encountering issues on other systems

I've encountered an issue with the code on my admin page. It used to work perfectly fine on my system, but now it seems to have stopped functioning. My client urgently needs to update this page, however, when I attempt to run it, the JQuery requests a ...

Use jQuery to retrieve the response from a JSON request and showcase it on the existing HTML page

Currently, I am working on a project that involves integrating a JSON-based web service from a remote server. The method of this service can be accessed by using specially formatted URLs such as: http://root-url/ws/service-name?request={json-string} The ...

What is causing the container to overlap with my sidebar-like <nav>?

Why is the fluid-container overlapping in this way, while others are not? Does this look correct to you, or am I overlooking something? It seems incorrect to me. <nav class="nav flex-column float-left"> {links} </nav> <div class="conta ...

Tips for validating a form with the assistance of jQuery

While there are multiple answers out there for this particular question, I am specifically interested in validating an entire form with dynamic input fields sourced from a database similar to Google Forms. HTML <div class="rs_card"><spa ...

Creating a table that adapts to different screen sizes using

I'd like to set up a layout where an input element is next to a button, occupying the entire width of the container. Perhaps using a table would be the way to go. In my HTML and CSS setup, I have the following: .css-table { display: table; } ...

Tips for executing jsfiddle code within Joomla 3.2

I'm having trouble executing this code on my Joomla website. I have Joomla 3.2 installed with the JCK Editor, but the HTML tags are not functioning correctly. Can someone please assist me in running the following code: $("#text10").keyup(functio ...

Adding a div with a canvas element is malfunctioning

I've been experimenting with using canvg to convert an SVG within a div into a canvas. So far, the conversion is working fine but when I try to copy the innerHTML of the div to another div, it's not functioning properly. The canvas is being gener ...

Rules of HTML tables extend to the border size

Check out this HTML code snippet: div { border: 2px solid red; height: 50vh; width: 50vw; } table { border: 1px solid; table-layout: fixed; } <div> <table rules="cols"> <tr> <th>Name</th> < ...

Guide on using PHP to remove the trash icon glyphicon

I am currently creating a data table that includes various actions, one of which is the option to delete a row from both the table and the database. My challenge lies in figuring out how to successfully delete a particular row by simply clicking on the Tr ...

Transitioning from Jquery to vanilla JavaScript or transforming Jquery code into pseudo code

Struggling with a snippet of jQuery code here. While I have a good grasp on JavaScript, my knowledge of jQuery is somewhat limited. I am seeking assistance to analyze the following code in order to translate it into plain JavaScript or pseudo code that ca ...