JavaScript JavaScript and the DOM Getting a Handle on the DOM A Simple Example

Antwaun Johnson
PRO
Antwaun Johnson
Pro Student 11,157 Points

Having trouble getting code to work

Greetings,

I am using an HP chromebook for the time being and I am having trouble the the code in this section. Since I started a few weeks ago, I have been able see mistakes and make corrections. I'm hopeful that another eye can see something I can not.

const myHeading = document.getElementById('myHeading')

myHeading.addEventListener('click', () => { myHeading.style.color = 'red'
});

3 Answers

james south
james south
Front End Web Development Techdegree Graduate 33,246 Points

with an arrow function, if there is only the return statement in the function body, the return is implied and neither the return keyword nor the curly braces are necessary, so try removing the curlies from the function body.

myHeading.addEventListener('click', () => myHeading.style.color = 'red');
Antwaun Johnson
Antwaun Johnson
Pro Student 11,157 Points

James,

Thanks for your help. I finally discovered that I had it linked wrong in the html file.

Charlie Harcourt
Charlie Harcourt
8,037 Points

So why in the example does the teacher use curly braces if they are not needed?

Thanks for the help :)

I could be wrong (I'm still learning too!), but I think you need a semi-colon after 'red'.

Yes I think that is the problem. Without the semi-colon, that line of code doesn't get seen as its own line of code. So it breaks there.

This is where the semi-colon is missed:

const myHeading = document.getElementById('myHeading')

and it should be:

const myHeading = document.getElementById('myHeading');

Charlie Harcourt
Charlie Harcourt
8,037 Points

He had the semi colon it was on the line below. It doesn't matter what line it is on as long as the function is correct and the relevant values are inside of it.

:)

Antwaun Johnson
PRO
Antwaun Johnson
Pro Student 11,157 Points

Thanks James, your input is greatly appreciated. I have tried removing the curly braces to no avail.

Charlie Harcourt
Charlie Harcourt
8,037 Points

If you are still struggling can you post your code. :)