8

I'm using jQuery to get the height of an element. But if the element doesn't exist, the following code will return NULL:

$height = $('#menu li.active ul').height(); // returns integer or null

Is it a cross-browser safe way for getting an integer value under every circumstance with the following code:

$height = $('#menu li.active ul').height() + 0;
pbaldauf
  • 1,671
  • 2
  • 23
  • 32
  • 1
    Why not check if the element exists first? --- Also `+ 0` will coerce a `null` to a `0`. – evolutionxbox Sep 02 '16 at 09:16
  • try with length first if length is > 0 then calculate the height – Sandeep J Patel Sep 02 '16 at 09:17
  • It really sounds like bad idea to use a height of 0 for non existing element. See [XY problem](http://meta.stackexchange.com/questions/66377/what-is-the-xy-problem). And if it was a not so bad idea, jQuery would already do it – A. Wolff Sep 02 '16 at 09:26

8 Answers8

19

There are many ways to deal with this. The one you describe, adding an integer value to coerce the type is fine. You could also convert to a number explicitly:

$height = Number($('#menu li.active ul').height());
// or:
$height = +$('#menu li.active ul').height();

Or you could use a logical operator as null coerces to false:

$height = $('#menu li.active ul').height() || 0;
Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339
4

It is safe, yes.

A shorter alternative:

$height = +$('#menu li.active ul').height();

(notice the + before the $)

Christoph
  • 1,631
  • 8
  • 19
3

Simplest way to do that..

$height = $('#menu li.active ul').height() || 0;

Here false value will be..

  • false
  • null
  • undefined
  • " "
  • 0
  • NaN
Ramdeo angh
  • 179
  • 9
2

This is a better approach infact...

if($('#menu li.active ul').length > 0){
   $height = $('#menu li.active ul').height();
}else{
    ......
}
Sandeep J Patel
  • 910
  • 9
  • 24
2

My short solution is: $height = $('#menu li.active ul').height() || 0.

If you want more descriptive solution, you can check other users' answers.

Edit: It depends on jQuery version too.

Levent Yumerov
  • 526
  • 1
  • 6
  • 15
0

Just check if the element exist:

var $height = 0;
if($('#menu li.active ul').length > 0){
  $height = $('#menu li.active ul').height();
}
Willi Pasternak
  • 456
  • 2
  • 12
0

You could check if $("#menu li.active ul") is not null:

$height = $('#menu li.active ul') != null ? $('#menu li.active ul').height() : 0;
Alessio Cantarella
  • 5,077
  • 3
  • 27
  • 34
0

I used the above-accepted answer and other exercises, which give me a nice result.

We can use with parseFloat or parseInt, which give 0 or the actual value. See demo specially last result :

function myFunction() {
  var a = parseFloat("10") + "<br>";
  var b = parseFloat("10.00") + "<br>";
  var c = parseFloat("10.33") + "<br>";
  var d = parseFloat("34 45 66") + "<br>";
  var e = parseFloat("   60   ") + "<br>";
  var f = parseFloat("40 years") + "<br>";
  var g = (parseFloat("1.1234") ||0) + "<br>";

  var n = a + b + c + d + e + f + g;
  document.getElementById("demo").innerHTML = n;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<p>Click the button to parse different strings.</p>

<button onclick="myFunction()">Try it</button>

<p id="demo"></p>
Ajay2707
  • 5,690
  • 6
  • 40
  • 58