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

Please help I do not get this question!!

I don't understand how to do this code challenge I did the previous one just fine. I would really appreciate the answer to this code challenge as I am limited on time. Thank you!

app.js
var sayButton = document.getElementById("say");
var greeting = function() {
  alert("Hello World!");
}
index.html
<!DOCTYPE html>
<html>
<head></head>
<body>
<button id="say">Say "Hello World!"</button>
<script src="app.js"></script>
</body>
</html>
Caleb Kleveter
Caleb Kleveter
Treehouse Moderator 37,862 Points

Sorry Justin, I have not done this course yet, and I don't know how to do this in vanilla JS.

4 Answers

Lido Fernandez
Lido Fernandez
10,556 Points

Hi Justin,

I do not remember the code challenge 100% but I think that the Script should between the <head> tags in the HTML and the JS doc should be something like this:

var greeting = function() {
    alert("Hello World!");
}

document.addEventListener("click", greeting);

I tried it and It works!

good luck!

Lido Fernandez
Lido Fernandez
10,556 Points

I am so sorry Justin. I think I took too much coffes today and I gave you a worng answer. This is the good one (I hope).

JS doc.

var sayButton = document.getElementById("say");

sayButton.onclick = greeting;

function greeting () {
    alert("Hello World!");
}

HTML doc

<!DOCTYPE html>
<html>
<head>

</head>
<body>
    <button id="say">Say "Hello World!"</button>
    <script src="app.js"></script>
</body>
</html>

Let me know how it goes

Lido Fernandez
Lido Fernandez
10,556 Points

By the way, you can also do it this way:

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

sayButton.onclick = greeting;
Caleb Kleveter
Caleb Kleveter
Treehouse Moderator 37,862 Points

Hey Lido,

Thought I would remind you to post your code with mark down, it makes it easier to read. In case you don't know how to do it, here is how it works:

3 back ticks (```), followed by the name of the language in all lower case (for iOS you use objective-c or swift, and for Android you use java), then below you post the code, and below that you use 3 more back ticks.

Lido Fernandez
Lido Fernandez
10,556 Points

Thanks a lot Caleb. I had no idea how to do that.