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

I have no idea what I'm doing

I'm totally lost here.

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

the problem is that you haven't defined a variable called p. and nothing is calling initElement. And you're assigning an element as if it were the callback.

2 Answers

Simon Coates
Simon Coates
28,694 Points

try

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

Events are attached on elements. The saybutton gets an element. The greeting creates a variable of function type. THe final line says, for this element, on a click event, do the functionality defined in greeting.

I magically figured it out but thanks. That is different than what I did. I believe I typed

var geeting =document.getElementById("Say");