Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

plain javascript to jquery - clientHeight

I have the following script

(function(win){
    var doc = win.document;
    if (doc.querySelector && doc.addEventListener) {
        var toggler = doc.querySelector('.toggle-menu')
        var menu = doc.querySelector('.main-nav ul');
        menu.style.height = '0px';
        toggler.addEventListener('click',function(e) {
            e.preventDefault();
            if (menu.style.height == '0px') {
                menu.style.height = 'auto';
                if (menu.clientHeight != 0) {
                    menu.style.height = menu.clientHeight+'px';
                }
            } else {
                menu.style.height = '0px';
            }
        });
    }
})(this);

What will be the jQuery version of that script, since i can't find a jQuery equivalent to clientHeight.

like image 734
alexandra Avatar asked May 03 '12 00:05

alexandra


People also ask

How do I get jquery clientHeight?

clientHeight can be calculated as CSS height + CSS padding - height of horizontal scrollbar (if present). I'm assuming that is the scrollbar of the element itself, not the entire browser window, unless the element takes up the entire window.

What is clientHeight in Javascript?

The clientHeight property returns the viewable height of an element in pixels, including padding, but not the border, scrollbar or margin.

How do I get the height of a div element?

Using clientHeight The clientHeight is a property which returns the height of the element in pixels. clientHeight − it includes the element's padding, but not its border, margin or horizontal scrollbar (if present). It can also include the height of pseudo-elements such as ::before or ::after.


2 Answers

clientHeight is not a jQuery property. It was introduced in Internet Explorer, but isn't part of the W3C specifications. It looks like it is only supported in Firefox and Internet Explorer. I've just tested that it works in the latest version of Chrome, though. Not sure if results are standard across browsers, though the link I posted below suggests no.

Also, Mozilla suggests the following formula to be used in place for browsers that don't support it:

clientHeight can be calculated as CSS height + CSS padding - height of horizontal scrollbar (if present).

I'm assuming that is the scrollbar of the element itself, not the entire browser window, unless the element takes up the entire window.

Sources:

  • clientHeight/clientWidth returning different values on different browsers
  • https://developer.mozilla.org/en/DOM/element.clientHeight
like image 189
Chris Laplante Avatar answered Sep 19 '22 21:09

Chris Laplante


There is .height(). It will return the height of an element.

var menu = $('.main-nav ul');
....
menu.height()+'px';
like image 21
Bakudan Avatar answered Sep 21 '22 21:09

Bakudan