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 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
.a{fill-rule:evenodd;}techdegree seal-36
Brandon White
Full Stack JavaScript Techdegree Graduate 35,771 Points

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) })