Obtain the precise value indicated in CSS by utilizing jQuery

Explanation

It's surprising that this question has never been asked before, but here we are... Please don't confuse it with this other question.

I'm attempting to save the height of an element using jQuery's data method so I can retrieve this value later and reset the original value on a specific element.

However, when I try to retrieve the height of my element, it gives me the computed height rather than the actual CSS value. While this is usually useful in other scenarios, I specifically need the exact value specified in my stylesheet, whether it's 100%, auto, or 10px...

My Question

Is there a way to obtain the precise CSS (non-computed) value of an element using jQuery?

For example (CSS):

#wrapper {
    height: auto;
}

And JS:

// Returns 'auto' not computed value...
var height = $('#wrapper').height();

Further Information

The only solution I can think of at the moment is to simply remove the inline style tag from my element, which will eliminate any styles applied by jQuery. The downside to this approach is that it will remove all styles, not just the specific one I'm interested in...

Answer №1

With jQuery, you are limited to retrieving the calculated value.

For an alternative approach, I have created a script that can determine the original CSS value by analyzing the stylesheets applied to the page, identifying the selector, and evaluating its precedence and specificity.

This method is useful for distinguishing between values such as auto, 100%, or fixed sizes.

Here is how you can use it:

resolveAppliedStyle(document.getElementById("wrapper"), "height");

You can download the script for resolveAppliedStyle from :

https://github.com/stephen-james/FixedHeaderTable/blob/master/dependencies/lib/resolveAppliedStyle.js

Additionally, the script uses another tool to calculate specificity, which can be found at :

https://github.com/keeganstreet/specificity/blob/master/specificity.js

Answer №2

Is it possible to obtain the precise CSS (non-calculated) value of an element using jQuery?

Currently, the only way to retrieve the computed value is in a unit such as px through the DOM.

However, one workaround is to determine the percentage by comparing the height of the element to its parent's height. Unfortunately, measurements like em, en, and others are not directly accessible.

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

Error message: Suitescript encountered an unexpected issue - TypeError: The function this.handleChange is not defined

For the past year, I have been immersed in Suitescript development. In my current project, I have a client script that triggers on Save for a Journal Entry. However, upon trying to save, I encounter an error message that reads "TypeError this.handleChang ...

The JavaScript window variable is not being returned from an AJAX request without a delay

At the beginning of my script, I set a default value for the name of a kmz file that I need for Google Maps. This value is stored in a window variable. window.kmz="Delaware_River_Basin.kmz" After running an ajax request, I receive the correct value for t ...

What is the purpose of using $ symbols within NodeJS?

Lately, I've been attempting to grasp the ins and outs of using/installing NodeJS. Unfortunately, I'm feeling a bit lost due to tutorials like the one found here and their utilization of the mysterious $ symbol. Take for instance where it suggest ...

Incorporating dynamic form elements using Vue.js within a targeted div

Here is the HTML and Vue.js code that I have: <table class="table"> <thead> <tr> <td><strong>Title</strong></td> <td><strong>Description< ...

Assign a property to an array of objects depending on the presence of a value in a separate array

Looking to manipulate arrays? Here's a task for you: const arrayToCheck = ['a', 'b', 'c', 'd']; We have the main array as follows: const mainArray = [ {name:'alex', code: 'c'}, ...

Concealing or revealing an image with jQuery when hovering

I currently have 3 <a> tags within my html, each containing 2 images. Specifically, the greyscale images are the ones that are visible while the colored ones are set to display:none. I am aiming to achieve a functionality where upon hovering over th ...

Is the data fetched by getStaticProps consistently the same each time I revisit the page?

When utilizing routes to access a specific page like page/[id].js, the concern arises whether data will be refetched each time the page is visited. For instance, if you navigate to another page through a link and then return to this original page by pres ...

Incorporating react-intl-tel-input into your project: A step

Greetings, I am currently delving into the realm of ReactJS and am tasked with incorporating react-intl-tel-input to capture phone numbers from around the globe. However, during the integration process, I encountered some obstacles. When I input this code: ...

A quick and easy way to locate the object tag along with the param, and insert the embed tag within an HTML document is

Struggling with understanding the simple HTML DOM of PHP? You're not alone. As a newcomer to the programming industry, following instructions can be challenging. I've been searching for how to locate the object tag and embed, but haven't fou ...

Firestore version 9 - Retrieve nested collection depending on a string being present in an array

Working on a new chat application here. It has been a while since I last used Firestore. I am currently using v9 and facing an issue with retrieving the nested "messages" collection when the "users" array in the document contains a specific ID. I have man ...

Retrieve the text content from a JavaScript alert using WebDriver

Currently, I am utilizing Selenium WebDriver in C# to enhance my knowledge and create automated tests. I recently encountered a particular scenario that has left me puzzled: Imagine you have a website similar to this one: . When attempting to register wit ...

Leveraging the power of angular's $asyncValidators by implementing a cache

I've created a validation directive that verifies a value against an endpoint App.directive('validate', function(fooService, $q) { return { restrict: "A", require: "ngModel", link: function(scope, elem, attrs, ngModel) { ...

Identifying the presence of a tag in Vue.js

Check out my Code: <template v-for="day in getMonthLength()" > <td :id='notempty' v-for="dataa in data" v-if="dataa.employee_id === employee.id"> <input type="numb ...

Utilizing a dynamic ref in Vue using the composition API

While working with the composition API in Vue 3, I am trying to create a reference to a component dynamically. Typically, this would involve adding ref="name" to the template and then defining a ref using const name = ref(null). However, I am loo ...

Endlessly scrolling text using CSS

Is it possible to create a continuous rolling text effect on an HTML page without any gaps, similar to a ticker tape display? For example, displaying "Hello World ... Hello World ... Hello World ... Hello World ..." continuously. I attempted using the CSS ...

Separate angular structure into various sections

I am developing a form builder using Angular dynamic form functionality. The form data is loaded from a JSON object, as shown below: jsonData: any = [ { "elementType": "textbox", "class": "col-12 col-md-4 col-sm-12", "key": "first_ ...

Ways to verify user login through jQuery post method

I am currently working on a login test application that utilizes jQuery post and PHP. I'm encountering an issue where the application is able to read the input values but fails to send the data to server.php and consequently does not return any data. ...

Improving React code by using conditional statements such as IF and Else

Currently, I am delving into the world of React and have managed to devise some logic for rendering a DIV. However, upon reflection, it has become evident that there is redundancy present in my code structure and I am uncertain about how to efficiently ref ...

Acquiring JSON-formatted data through the oracledb npm package in conjunction with Node.js

I am currently working with oracledb npm to request data in JSON format and here is the select block example I am using: const block = 'BEGIN ' + ':response := PK.getData(:param);' + 'END;'; The block is ...

It never fails to function automatically, no matter which script is being executed

By default, the script will always be executed regardless of its environment. Check out my code snippet: import { Pool } from 'pg'; import config from './../config'; const connectionString = () => { switch (process.env.NODE_EN ...