Jump to content

JavaScript Problem

robeng
Go to solution Solved by Mooshe,

document.getElementsByTagName() does not return an HTMLElement, but rather an array of elements. You would need to iterate through the list to modify the style of each element:

 

for(h in document.getElementsByTagName("header")) {
  h.style.top = ...;
}

if you just want to edit one specific element, I would recommend providing an ID attribute to that element to select it.

Hi! 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!

Link to comment
Share on other sites

Link to post
Share on other sites

document.getElementsByTagName() does not return an HTMLElement, but rather an array of elements. You would need to iterate through the list to modify the style of each element:

 

for(h in document.getElementsByTagName("header")) {
  h.style.top = ...;
}

if you just want to edit one specific element, I would recommend providing an ID attribute to that element to select it.

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×