Getting the height of a group of classes using the style attribute

How can I retrieve the current height of 813px in jQuery from the "style" section?

An example of F12 CSS is shown here: https://i.sstatic.net/4wFfR.jpg

My attempt at achieving this is as follows:

function HandleAccordionControlSizeChanges(isOpened) {
    var currentHeight = 0;

    if (isOpened) {
        currentHeight = $(".dx-accordion-item-opened").height();
    }
    else {
        currentHeight = $(".dx-accordion-item-closed").height();
    }

    return currentHeight;
}

Now, I need to extract the style tag that contains the value 813px.

Answer №1

Take a look at this response: How to read and change inline styling "height" using jquery?

You can use the .css() method.

However, according to the official jquery documentation, it is recommended to use ".height()" when performing mathematical operations:

Official jquery documentation on .height()

The difference between using .css( "height" ) and .height() lies in the fact that the latter returns a unit-less pixel value (e.g., 400), while the former retains the units (e.g., 400px). It is advised to use the .height() method when the element's height needs to be used in calculations.

Answer №2

If you set the height using CSS, you have different options to retrieve it in jQuery. You can use .css('height') if you want the value with 'px' appended, or .height() for the raw integer value:

console.log($('.element').css('height'));
console.log($('.element').height());
.element {
  height: 813px;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<div class="element">Example</div>

It's important to note that these methods only work on the existing (computed) value. If you set the height dynamically in JavaScript, they won't be able to retrieve it.

To get the height of an element with inline styles, you can use .attr() in jQuery. Alternatively, you can target the actual HTMLElement itself (with [0]) and access the raw style property directly with .style.height:

console.log($('.element').attr('style'));
console.log($('.element')[0].style.height);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<div class="element" style="height: 813px;">Example</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

The inline display property does not function properly with the <li> element

Here is the HTML code snippet I am working with: <div class="nav"> <ul class="nav-dots"> <li class='ni n1'><a href="" class='nia'>dot</a></li> <li class='ni n2'><a href="" clas ...

Converting Umbraco Json to a CSS class named "[]"

I'm currently using Umbraco with the data-type grid layout and I am trying to add custom settings (CSS classes) to each row/cell. The results are somewhat working, but I want to improve it. Here's the user interface: https://i.stack.imgur.com/iY ...

"The sliding function of the React Bootstrap carousel is malfunctioning as it goes blank just before transitioning

Here is the code snippet I am working with: Whenever the carousel transitions to the next image, the current image disappears before displaying the next one. I am using react-bootstrap version 5.1.0, but it seems like there may be an issue with the transi ...

Font sizes may vary depending on whether the site is accessed with or without the "www" prefix

There is an odd occurrence on my website where the font size seems to change when viewing both the www version and non-www version. While setting up a 301 redirect is a common solution, I am intrigued by why this discrepancy exists as it is not something ...

What is the best way to create a slideshow that automatically starts upon loading and pauses when hovered over?

I have recently set up a div element for my slideshow. I've included a script to enable navigation using next and previous arrows. However, I am looking to add an automatic slideshow feature that stops on hover. As a beginner, I would appreciate any a ...

A complete div component with a minimum height that is sandwiched between a fixed size header and

I'm currently facing a challenge with setting up my webpage due to the height of my elements causing a bit of a frenzy. Specifically, I have a specific layout in mind: At the top, there should be a header that has a fixed height (let's say 150p ...

The navigation menu dissolves into hiding at the uppermost part of the page upon scrolling upwards on iOS devices

I am currently working on creating a navigation menu that will automatically hide when scrolling down and reappear when scrolling up. This functionality works perfectly fine on desktop and Android browsers, but I have encountered an issue specifically with ...

Swapping the non-DOM element text with another content

Currently facing an issue in my project where I need to replace plain text inside a contenteditable element without being enclosed in a DOM element. Here, I'm extracting the textNode using window.getSelection(); and looking to perform a text replaceme ...

Resetting several sticky titles after displaying and hiding elements

Inspired by a popular codepen example, I have successfully implemented sticky titles in my sidebar. However, when integrating these sticky titles with the functionality to show/hide related items on click, I encountered some unexpected issues. The code sni ...

The output I receive when making a jQuery POST request is a response generated by my index controller, although I am not specifically directing it there as I have

Currently, I am working with the latest version of codeigniter. After sending a post request, all I am receiving back is my view. This situation has left me puzzled. Listed below are my routes: $route['default_controller'] = "veebn"; $route[&a ...

Difficulty arises from the syntax error in incorporating the setTimeout function into an already existing jQuery script

I am looking to incorporate a setTimeout function into the code below in order to introduce a brief pause before the fadeOut effect takes place. $(document).ready(function() { $('#menu li').hover( function() { $('u ...

The consistency of values remains constant throughout all Ajax requests

I am presenting the code snippet below: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8> <title>Document</title> <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js" ...

Container with adjacent gradient CSS shape

I am currently working on a unique design concept for a website we are developing. The header requires a parallelogram shape above it and a trapezium shape to the right of the container, as illustrated below. Successfully implementing the parallelogram ab ...

What's the best way to undo a CSS transition animation when deleting an active class?

I'm currenty working on a straightforward icon animation within a VueJS application. I've implemented a Vue transition to create a fade effect on the background elements, but I'm exploring options for a subtle upward shift animation specific ...

Utilizing CSS to Implement a Background Image

Here is where I am looking to upload the image. Ideally, I would like to position my image to the left side of the text related to Food and Travel. You can view the image here. .block-title h3 { color: #151515; font-family: 'Montserrat', s ...

The problem with SVG animation using Velocity.js is that it is throwing an error stating that

I'm attempting to design an animated logo using SVG and Velocity.js, but I'm encountering an error in Chrome. "$ is not defined" This issue seems to be related to script loading order, but I can't pinpoint my mistake. Safari runs the proj ...

Attach a Material-UI Popper component to a customized edge label in a React Flow

After finding inspiration from this particular example in the react-flow documentation, I decided to create my own customized version. It showcases a Material Ui Popper that appears when the edge is selected. However, my problem arises when attempting to z ...

Switching between various quantities of inputs in each row using Bootstrap V3

My Bootstrap V3 form has a mix of rows with two inputs and rows with one input, as per the designer's request. Check out my fiddle here: https://jsfiddle.net/06qk4wh4/ <div class="form-group col-sm-12"> <label class="control-label col- ...

Troubleshooting CSS issues in HTML pages

I created an HTML file but the CSS properties are not displaying correctly in the browser. I'm not sure where I made a mistake. Instead of using a separate CSS file, I have included it directly in the HTML file. <!DOCTYPE html> <html> &l ...

Trying to work through solving the issue of accessing document.frames["someframe"].function in Chrome instead of IE

I am encountering an issue with my code that is working in Internet Explorer but not in Chrome. try { top.document.frames["myFrame"].compare(); } catch(err) { alert("This is not executed."); } Any suggestions on how to resolve this compatibility pr ...