Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to change the offsetHeight of a element using javascript?

Hello i'm trying to change the offsetHeight of an element. i used the following

document.getElementById('id').style.offsetHeight = 0;

but i saw no visible change. Can anyone help me please?

like image 501
harry Avatar asked Feb 07 '11 18:02

harry


People also ask

Can JavaScript change the style of an element?

The HTML DOM allows JavaScript to change the style of HTML elements.

What is offsetHeight in JavaScript?

The HTMLElement. offsetHeight read-only property returns the height of an element, including vertical padding and borders, as an integer. Typically, offsetHeight is a measurement in pixels of the element's CSS height, including any borders, padding, and horizontal scrollbars (if rendered).

How can you change the height of an element?

To change the height of a HTML Element using JavaScript, get reference to this HTML Element element, and assign required height value to the element. style. height property. In the following example, we will change the height of HTML Element with id "myElement" to "150px" , in JavaScript, using element.

How do you change an element in JavaScript?

First, select the DOM element that you want to replace. Then, create a new element. Finally, select the parent element of the target element and replace the target element by the new element using the replaceChild() method.


2 Answers

The offsetHeight property indicates the height of the visible area for an element. It's a shorthand that contains the sum of dimensions from padding, scrollbars and borders.

However, it can't be used to change the actual size and as noted in comments, offsetHeight is a property of an element, not style.

To modify the actual size use height, padding or border.

like image 168
Saul Avatar answered Sep 23 '22 17:09

Saul


You should set style.height to a string ending in px.

like image 38
SLaks Avatar answered Sep 22 '22 17:09

SLaks