-3

I am trying to set the height of an element dynamically, so that when the document first opens, the element takes up the whole screen (adjusted for other elements on the screen - not shown in the snippet below).

I am using JQuery in my code, and the code shown below is called after the document is loaded as per the standard JQuery pattern:

$(function({ 
    /* called here! */
});

However, I found that the results are the same - with or without JQuery, so for simplicity of the code, I have shown code that using jQuery:

<style>
  #foo {
    border: 1px solid red;
  }
</style>
<div id="foo"></div>
<div id="foobar"></div>
<script>
  const elem = document.getElementById('foo');
  console.log('Element:' + elem);
  console.log(`Before: ${elem.style.minHeight}`);
  elem.style.minHeight = 1500;
  console.log(`After: ${elem.style.minHeight}`);
</script>

How do I adjust the height of the foo element dynamically? I used both the Height and minHeight attributes, and the results are the same.

In both cases, the elemnt is correctly identified, but the attribute cannot be accessed - Before and After are both empty

2
  • Have you referred to this: stackoverflow.com/questions/18994830/…? Commented Dec 10, 2020 at 13:59
  • elem.style.minHeight = "1500px"; so likely voted down because your code was not syntactically correct to begin with and there were dupes Commented Dec 10, 2020 at 14:13

2 Answers 2

2

Since the element hasn't had a height, you'll need to check the offsetHeight. Take a look at Why browser is returning empty string on style.height ? How to get actual height of an element?

Secondly, to set the height of the element, you should include px after 1500: elem.style.height = '1500px';.

An other option is to use: setAttribute('style', 'height:1500px');

<!DOCTYPE html>
<html>
    <head></head>
    <body>
        <style>
            #foo {
                border: 1px solid red;
            }
        </style>
        <div id="foo"></div>
        <div id="foobar"></div>
        <script>
            const elem = document.getElementById('foo');
            console.log('Element:' + elem);
            console.log(`Before: ${elem.offsetHeight}`);
            elem.style.height = '1500px';
            console.log(`After: ${elem.style.height}`);
        </script>
    </body>
</html>


Since you've tagged Jquery, it's height function used the pixels by default, so you could shorten the code to:

const elem = $('#foo');
console.log(`Before: ${elem.height()}`);
elem.height(1500);
console.log(`After: ${elem.height()}`);

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<!DOCTYPE html>
<html>
    <head></head>
    <body>
        <style>
            #foo {
                border: 1px solid red;
            }
        </style>
        <div id="foo"></div>
        <div id="foobar"></div>
        <script>
            const elem = $('#foo');
            console.log(`Before: ${elem.height()}`);
            elem.height(1500);
            console.log(`After: ${elem.height()}`);
        </script>
    </body>
</html>

Sign up to request clarification or add additional context in comments.

3 Comments

Please let OP clarify the question. In his first sentence he is talking about I am trying to set the width of an element dynamically where later on he's talking about I used both the Height and minHeight attributes, and the results are the same..
And indeed, the title is "setting the height"
It was a typo. Fixed now
1

1500 is not a valid value for minHeight on its own - you need to specify the units, eg "px" or "%".

Updated snippet:

<style>
  #foo {
    border: 1px solid red;
  }
</style>
<div id="foo"></div>
<div id="foobar"></div>
<script>
  const elem = document.getElementById('foo');
  console.log('Element:' + elem);
  console.log(`Before: ${elem.style.minHeight}`);
  elem.style.minHeight = "1500px";
  console.log(`After: ${elem.style.minHeight}`);
</script>

2 Comments

Fair point, it was a throwaway value I used to see what point scale it was using and set it to something very large so I could see if t had any effect.
It's not the size that matters.... 1500 is not allowed, it needs to be 1500px

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.