0

I have a javascript file that is beaing loaded last. In the file this code is executed:

var x = document.getElementById("bildfram1").offsetHeight;

document.getElementsByTagName("header").style.top = "calc(x/2)";

But I get this error in the console:

Uncaught TypeError: Cannot set property 'top' of undefined

Any help on how to fix this! Thanks in advance!

Mi-Creativity
  • 9,554
  • 10
  • 38
  • 47
  • 2
    Possible duplicate of [Javascript | Uncaught TypeError: Cannot set property 'color' of undefined](http://stackoverflow.com/questions/30232423/javascript-uncaught-typeerror-cannot-set-property-color-of-undefined) – Heretic Monkey Jan 16 '17 at 17:45
  • 1
    Possible duplicate of [What do querySelectorAll, getElementsByClassName and other getElementsBy\* methods return?](http://stackoverflow.com/questions/10693845/what-do-queryselectorall-getelementsbyclassname-and-other-getelementsby-method) – Sebastian Simon Jan 16 '17 at 17:48

1 Answers1

0

getElementsByTagName returns an HTMLCollection of elements (an array-like object).

For this reason, you are required to target specific index(es).

document.getElementsByTagName('header')[0].style.top = "calc(x/2)"

calc is an experimental technology. If all you have to do a simple divison, you should probably do it beforehand.

var division = val/2;
document.getElementByTagName('header')[0].style.top = divison + 'px'
zurfyx
  • 31,043
  • 20
  • 111
  • 145