8

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 only want a height value if it is specifically declared in a CSS file for that element.

Like if it isn't declared anywhere, perhaps it could return 'auto' like it does for top and left sometimes...

For example, my CSS looks like this:

.element{
    margin-left:5px;
    color:red;
}

.css("margin-left") returns "5px", while .css("height") returns 20 even though it isn't specifically set...

Thanks

allicarn
  • 2,859
  • 2
  • 28
  • 47
Nate L
  • 465
  • 1
  • 5
  • 17
  • Seems like a duplicate of this question: http://stackoverflow.com/questions/4105355/jquery-javascript-csswidth-check-if-style-is-defined-in-css – Tomer Arazy Apr 21 '13 at 00:29
  • Not a duplicate question. But, definitely one of the answers seems to be answering this question. http://stackoverflow.com/a/4105468/672455 – hop Apr 21 '13 at 00:32

3 Answers3

0

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;
}
Ozerich
  • 2,000
  • 1
  • 18
  • 25
  • 1
    The question is if there is no style set _inline OR in the style sheet_ it should return "auto". I put your code in a [jsfiddle](http://jsfiddle.net/8R6m5/). With the `.get(0).style` it only returns something if it is set inline. With jQuery's `.height()`, it returns the computed value. – allicarn Oct 22 '13 at 18:44
0

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;
}
<div id="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>
Code_Ninja
  • 1,729
  • 1
  • 14
  • 38
0

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.

Dharman
  • 30,962
  • 25
  • 85
  • 135
Jafer.balti
  • 584
  • 3
  • 14