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 Callback Functions in JavaScript Callbacks and the DOM Callbacks with DOM Elements

Goal: Apply the spinElement callback to btn1, btn2, and btn3. The spinElement function should trigger on click event!

Please help!

app.js
const btn1 = document.getElementById("button1");
const btn2 = document.getElementById("button2");
const btn3 = document.getElementById("button3");

function spinElement(event) {
  //Applies spinning animation to button element
  event.target.className = "spin";
}
index.html
<!DOCTYPE html>
<html lang="en">
    <head>
        <title></title>
        <link rel='stylesheet' href='styles.css'>
    </head>
    <body>
        <section >
            <button id='button1'>Button 1</button>
            <button id='button2'>Button 2</button>
            <button id='button3'>Button 3</button>
        </section>
        <script src='app.js'></script>
    </body>
</html>

2 Answers

Brandon White
seal-mask
MOD
.a{fill-rule:evenodd;}techdegree seal-36
Brandon White
Treehouse Moderator

Hi Sean,

What you'll want to do is call the addEventListener method on the button elements. The addEventListener has at least three parameters. The first is a string which represents the event you want the browser to listen for, the second is a callback function (or a function that is called once the event is triggered).

Here's a link to an article about the addEventListener method. https://developer.mozilla.org/en-US/docs/Web/API/EventTarget/addEventListener

Hopefully, that info is enough of a tip to get you looking in the right direction. If you end up needing a bit more of a hint than that, let me know, and I'll do my best.

Here's another hint: You don't need to alter any of the code presented to you by the challenge. You'll simply add new statements to the code that's already there.

{button}.addEventListener(‘click’, (e)=>{ spinElement(e) })