Hello. I’m having issues w/ an exercise regarding getElementById. I’m given an HTML doc w/ an image and header. Here is the HTML code.
I ♥ unicorns
We are also given an area of JS file to type in the getElementById. After I tried, it said I was missing. Here is what I have so far.
let unicorn = document.getElementById(unicorn); let mianheading = document.getElementById(mainheading);
Select the image element by its ID and save it to a variable called ‘image’. And
Select the H1 by its ID and save it to a variable called ‘heading’.
I think I’m only mis labeling the name. Or tying the wrong name. I’m not sure. Would love the help. Thanks
getElementById takes a string for an argument. So unless you have set the variables
mainheading to a string before you pass them into
getElementById then you are just going to
null for a return value.
Also, the elements you are trying to get have to have an
id attribute on them in the first place. Their
id is the string you want to pass into
There is an element
id on them from what I can see. That is already setup. Unless it isn’t and I have to alter the HTML code as well?
I was just going by the HTML you posted above. Both the
h1 and the
img do not have
ids, at least according to the dev tools inspector.
Regardless, make sure the string you pass into
getElementById is the id for the element you are trying to get.
Oh I wasn’t aware they don’t/didn’t have
id's on either. I thought they did. Right that’s what I thought. The string name for the passed in id name is what I want.
I think you should use a string … Something like this:
let unicorn = document.getElementById(“unicorn”);
I was thinking that as well but wasn’t sure. Thanks.
Update: So apparently it’s been correct this whole time and I’ve just been saving the variable w/ a different name than the instructions wanted.
So yeah, this is an ADD issue when it comes to reading the instructions and code properly/fully.
This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.