Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to get and set div.class height

Please suggest me any way to get and update the height of a HTML element in typescript.

document.getElementsByClassName('height').style.height = childHeight;

is not working. I get

Property 'style' does not exist on type 'HtmlCollectionOf<Element>'

like image 469
Rajesh Kumar Avatar asked Jan 17 '17 06:01

Rajesh Kumar


People also ask

How can we get dynamic height of div?

We use css property height: calc( 100% - div_height ); Here, Calc is a function. It uses mathematical expression by this property we can set the height content div area dynamically.

Can div have height?

This seems like the ideal candidate for transition from a table-based layout to a CSS layout. It makes sense: both DIVs and tables can be nested, have HEIGHT and WIDTH attributes set, contain borders, etc.

How to get the <Div> height using JavaScript?

How to get the <div> height using JavaScript ? Method 1: Using the offsetHeight property: The offsetHeight property of an element is a read-only property and used to return the height of an element in pixels. It includes any borders or padding of the element.

What is the use of height in CSS?

CSS Setting height and width The height and width properties are used to set the height and width of an element. The height and width properties do not include padding, borders, or margins. It sets the height/width of the area inside the padding, border, and margin of the element.

How to get the height and width of an element using jQuery?

We can also use the jQuery height () and width () methods to get the height and width of the element. This height and width do not include border, padding and margin. Let’s see how we can return the height and width of a <div> element.

How to get the inner height and width of an element?

The jQuery innerHeight () and innerWidth () methods get the inner height and width of an element. Inner height and width include padding.


2 Answers

One problem here might be that getElementsByClassName returns an array of elements with class height. To apply style method you need to have one element.

This code will apply 10px height to the first such element:

document.getElementsByClassName('height')[0].style.height = '10px';
like image 139
Bhabani P Avatar answered Oct 23 '22 03:10

Bhabani P


First, the code you are after:

const cnElems = document.getElementsByClassName("cn");

for (let i = 0; i < cnElems.length; i++) {
    const e = cnElems[i];
    if (e instanceof HTMLElement) {
        e.style.height = "10px";
    }
}

Then, an explanation... There are two main obstacles here:

  • getElementsByClassName returns a HTMLCollection<Element>
    for older targets and browsers this collection needs to be enumerated with an old-school for loop. For newer targets (>= ES6) and browsers, see this answer.
  • the elements of the collection are of type Element but style is defined on HTMLElement. Luckily TS automatically narrows a type in the scope of a type guard.
like image 32
CaringDev Avatar answered Oct 23 '22 05:10

CaringDev