Skip to content Skip to sidebar Skip to footer

Return Css Height With Jquery, Not Computed, But Declared

I think this might have been the default in a previous version of jquery, but when I call .css('height') and .height(), it returns the computed height, which isn't what I want- I o

Solution 1:

The browser automatically calculates CSS height and width.

You can see them, for example, in tab "Computed styles" in Chrome Developers Tools(F12)

And docs:

jQuery.width()

Get the current computed width for the first element in the set of matched elements or set the width of every matched element.

The difference between .css(width) and .width() is that the latter returns a unit-less pixel value (for example, 400) while the former returns a value with units intact (for example, 400px)

But if you want to get correct CSS value, i can advise don't use jQuery

if we have HTML:

<div id="elem" style="height: auto"></div>

we can write JS:

$('#elem').get(0).style.height    // "auto"

if we have HTML:

<div id="elem"></div>

JS:

$('#elem').get(0).style.height    // ""

universal function:

var height = function(elem){
      return $(elem).get(0).style.height === "" ? $(elem).height() : $(elem).get(0).style.height;
}

Solution 2:

All you have to do is provide an id to the element, and then try this code:

if (document.getElementById("element").style.height == null || document.getElementById("element").style.height == "") {
  alert("no height");
}else{
  alert(document.getElementById("element").style.height);
}
.element {
  width: 100%;
  color: red;
  word-break: break-all;
}
<divid="element"class="element">
  Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Donec quam felis, ultricies nec, pellentesque eu, pretium quis,
  sem. Nulla consequat massa quis enim. Donec pede justo, fringilla vel, aliquet nec, vulputate eget, arcu. In enim justo, rhoncus ut, imperdiet a, venenatis vitae, justo. Nullam dictum felis eu pede mollis pretium. Integer tincidunt. Cras dapibus. Vivamus
  elementum semper nisi. Aenean vulputate eleifend tellus. Aenean leo ligula, porttitor eu, consequat vitae, eleifend ac, enim. Aliquam lorem ante, dapibus in, viverra quis, feugiat a, tellus. Phasellus viverra nulla ut metus varius laoreet. Quisque rutrum.
  Aenean imperdiet. Etiam ultricies nisi vel augue. Curabitur ullamcorper ultricies nisi. Nam eget dui. Etiam rhoncus. Maecenas tempus, tellus eget condimentum rhoncus, sem quam semper libero, sit amet adipiscing sem neque sed ipsum. Nam quam nunc, blandit
  vel, luctus pulvinar, hendrerit id, lorem. Maecenas nec odio et ante tincidunt tempus. Donec vitae sapien ut libero venenatis faucibus. Nullam quis ante. Etiam sit amet orci eget eros faucibus tincidunt. Duis leo. Sed fringilla mauris sit amet nibh.
  Donec sodales sagittis magna. Sed consequat, leo eget bibendum sodales, augue velit cursus nunc,
</div>

Solution 3:

You can do it in JavaScript using class name as well. For example:

alert(document.getElementsByClassName("YourClassName")[0].style.height)

It will alert the actual value of height rather returning the computed height.

Post a Comment for "Return Css Height With Jquery, Not Computed, But Declared"