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 trialIulia Mihet
5,770 PointsI 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.
var sayButton = document.getElementById("say");
var greeting = function() {
alert("Hello World!");
}
sayButton.onclick(greeting());
<!DOCTYPE html>
<html>
<head></head>
<body>
<button id="say">Say "Hello World!"</button>
<script src="app.js"></script>
</body>
</html>
2 Answers
Alex Heil
53,547 Pointshey 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 ;)
Seth Kroger
56,413 PointsIt's specifically asking you to use assignment, ie. =, to do set the button's onclick:
sayButton.onclick = greeting;
Iulia Mihet
5,770 PointsIulia Mihet
5,770 PointsAlex Heil thanks for your help!
Alex Heil
53,547 PointsAlex Heil
53,547 Pointsyou're welcome Iulia - glad to hear that this helped you out ;)