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

Iulia Mihet
Iulia Mihet
5,770 Points

I wrote 3 different valid solutions in the coding challenge, and neither is accepted by the program. What to do?

First, I wrote

sayButton.on ("click", greeting()); sayButton.onclick(greeting()); sayButton.click(greeting());

I know they work, because the alert window appears every time, saying "Hello World!"... This is very frustrating.

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

2 Answers

Alex Heil
Alex Heil
53,547 Points

hey Iulia Mihet, no worries - we'll get that sorted out ;) so let's walk through the question: After line 4 in the app.js file, assign the greeting event handler to the click event on the sayButton.

part1: greeting should be assigned to something, this would look like this:

greeting = 

part2: we want the click event handler of the sayButton, like this:

sayButton.onclick

now when we bring them together it will work fine:

greeting = sayButton.onclick;

hope that helps and have a nice day ;)

Alex Heil
Alex Heil
53,547 Points

you're welcome Iulia - glad to hear that this helped you out ;)

Seth Kroger
Seth Kroger
56,413 Points

It's specifically asking you to use assignment, ie. =, to do set the button's onclick:

sayButton.onclick = greeting;