0

This might be a very stupid question for some people, but I surely am not able to understand the problem behind this. The code is fairly simple, and goes as follows:

HTML:

<!DOCTYPE html> <html lang="en-US"> <head> <title>####</title> <meta charset="UTF-8"> <link rel="stylesheet" type="text/css" href="css.css"> </head> <body onload="resizeDiv();"> <div id="testElement">ABCD</div> <div id="secElement">ABC</div> <script type="text/javascript" src="javascript.js"></script> </body> </html> 

CSS:

html , body{ height:100%; } #testElement{ background-color:black; color:white; } #secElement{ font-size:50px; } 

JS:

(function immediate(){ document.getElementById('secElement').textContent = window.innerHeight; }()); function resizeDiv(){ document.getElementById('testElement').style.height = (window.innerHeight - 50) * 0.9; } 

Now, by this code, the div with id 'testElement' should have an height which is 90% of the window.innerHeight - 20. But, nothing seems to work. Please help.

2
  • You have to wrap the JS in a handler like window.onload(function{}) for the ready or loaded events. When your JS is executed right now, the element doesn't yet exist. Commented Oct 31, 2015 at 16:54
  • @BBrown, I tried to include the function in window.onload, but still no response Commented Oct 31, 2015 at 17:00

3 Answers 3

2

Add "px":

document.getElementById('testElement').style.height = (window.innerHeight - 50) * 0.9 + "px"; 
Sign up to request clarification or add additional context in comments.

Comments

1

body tag has a onload handler assigned to resizeDiv() but your script is at the bottom of the page so, the onload handler can't refer to it.

Comments

1

Use document.getElementById('testElement').setAttribute("style","height:" + (window.innerHeight - 50) * 0.9) + ' px'; You can find more explanation why to use it here: Setting DIV width and height in JavaScript

3 Comments

Did you replace height with (window.innerHeight - 50) * 0.9?
i did everything you mentioned, and by the above answer, I guess the problem was that 'px' string. Anyways, thnx for the help
I forgot to add the ' px' part

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.