Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript Interactive Web Pages with JavaScript Selecting Elements and Adding Events with JavaScript Adding Events

Robbie Thomas
Robbie Thomas
31,093 Points

Adding Events

I'm very confused on this one, and also with the video that goes along with this code challenge, I think Andrew Chalkey (Spelled his name wrong more than likely) was also confused while showing us how to do it. Help?

app.js
var sayButton = document.getElementById("say");
var greeting = function() {
  alert("Hello World!");
}
  greeting.onClick = say;
index.html
<!DOCTYPE html>
<html>
<head></head>
<body>
<button id="say">Say "Hello World!"</button>
<script src="app.js"></script>
</body>
</html>

3 Answers

Hi Robert,

Line 5 :smiley: - so the full challenge would be:

var sayButton = document.getElementById("say");
var greeting = function() {
  alert("Hello World!");
}
sayButton.onclick = greeting;

Hi Robert,

You were close.

Your button is assigned to the variable sayButton and our function is within the variable greeting, so when we click the sayButton we want to run greeting().

We can do this by:

sayButton.onclick = greeting;

Note the lower case of onclick as opposed to onClick. In your current attempt you were trying to call an undefined item say on the function greeting (instead of the button sayButton).

Good luck, and happy coding.

KB :octopus:

Robbie Thomas
Robbie Thomas
31,093 Points

I tried that, still giving me the bummer message. This needs to be on line 4, correct?