Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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,692 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,692 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");