# Add a specific event listener based on button text

**URL:** <https://forum.freecodecamp.org/t/add-a-specific-event-listener-based-on-button-text/73228>\
**Category:** General\
**Created:** [January 6, 2017, 6:52am UTC](https://forum.freecodecamp.org/t/add-a-specific-event-listener-based-on-button-text/73228 "2017-01-06T06:52:35Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![JackEdwardLyons](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/jackedwardlyons/32/158616_2.png) [@JackEdwardLyons](https://forum.freecodecamp.org/u/JackEdwardLyons)\
**Post date:** [January 6, 2017, 6:52am UTC](https://forum.freecodecamp.org/t/add-a-specific-event-listener-based-on-button-text/73228/1 "2017-01-06T06:52:35Z")

</div>

I’m working on my Simon Game and was wondering if it is possible to add a specific event listener to a button based it’s innerText?

For example. I have a button saying “START GAME”. It gets clicked, a countdown from 3, 2, 1 occurs.  
Then I want the button to switch to “RESTART” and I would like the functionality for the restart function to be set. Here’s my code thus far…

```
    startGame.addEventListener('click', () => {
      startGame.style.pointerEvents = "none";
      let time = 4;
      let countDown = () => {
               time--;
               startGame.innerText = time;
               
               if (time < 1) {
                 startGame.innerText = "GO!";
                  clearInterval(counter);
                  
                  setTimeout(() => {
                    startGame.innerText = "RESTART";
                    // do i detach the event handler and write a new one here?
                  }, 2000);
               }
            };
       const counter = setInterval(countDown, 1000);
     });
```

---

<div class="post-metadata">

**Author:** ![PortableStick](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/portablestick/32/65591_2.png) [@PortableStick](https://forum.freecodecamp.org/u/PortableStick)\
**Post date:** [January 6, 2017, 3:21pm UTC](https://forum.freecodecamp.org/t/add-a-specific-event-listener-based-on-button-text/73228/2 "2017-01-06T15:21:21Z")

</div>

If you can dream it, you can code it ✨ ⭐ ✨

You’re thinking about this the wrong way, though. You don’t need to change the event handler, you just need to toggle your application’s state.

```auto
if (game.counter) {
    clearInterval(game.counter);
    this.innerText = "GO!";
} else {
    game.counter = setInterval(countdown, 1000);
    this.innerText = "RESTART";
}

```

---

<div class="post-metadata">

**Author:** ![JackEdwardLyons](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/jackedwardlyons/32/158616_2.png) [@JackEdwardLyons](https://forum.freecodecamp.org/u/JackEdwardLyons)\
**Post date:** [January 7, 2017, 7:44am UTC](https://forum.freecodecamp.org/t/add-a-specific-event-listener-based-on-button-text/73228/3 "2017-01-07T07:44:11Z")

</div>

Interesting solution, thanks.

In the end I found another solution which I thought was pretty cool. Let me know if you think there are any drawbacks…

```
startGame.addEventListener('click', function() {
    if (this.innerHTML === 'START GAME') {
      countDownToBegin.call(this);
    } else if (this.innerHTML === 'RESTART') {
      this.innerHTML = 'START GAME';
      return;
    }
  });
```

---

<div class="post-metadata">

**Author:** ![PortableStick](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/portablestick/32/65591_2.png) [@PortableStick](https://forum.freecodecamp.org/u/PortableStick)\
**Post date:** [January 7, 2017, 3:10pm UTC](https://forum.freecodecamp.org/t/add-a-specific-event-listener-based-on-button-text/73228/4 "2017-01-07T15:10:53Z")

</div>

You need to clear the timer before you can start another. The way you have it now, you’ll be creating multiple timers.

---

<div class="post-metadata">

**Author:** ![JackEdwardLyons](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/jackedwardlyons/32/158616_2.png) [@JackEdwardLyons](https://forum.freecodecamp.org/u/JackEdwardLyons)\
**Post date:** [January 8, 2017, 2:46am UTC](https://forum.freecodecamp.org/t/add-a-specific-event-listener-based-on-button-text/73228/5 "2017-01-08T02:46:17Z")

</div>

Yeah, but the button needs to reset, because I want the user to be able to restart the game if they wish. Hence, another 3, 2, 1 counter will be called…

---

<div class="post-metadata">

**Author:** ![PortableStick](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/portablestick/32/65591_2.png) [@PortableStick](https://forum.freecodecamp.org/u/PortableStick)\
**Post date:** [January 8, 2017, 3:06am UTC](https://forum.freecodecamp.org/t/add-a-specific-event-listener-based-on-button-text/73228/6 "2017-01-08T03:06:27Z")

</div>

Sure, but unless the button stops the previous timer, the user could create a bunch of useless timers.

---

<div class="post-metadata">

**Author:** ![JackEdwardLyons](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/jackedwardlyons/32/158616_2.png) [@JackEdwardLyons](https://forum.freecodecamp.org/u/JackEdwardLyons)\
**Post date:** [January 8, 2017, 3:14am UTC](https://forum.freecodecamp.org/t/add-a-specific-event-listener-based-on-button-text/73228/7 "2017-01-08T03:14:22Z")

</div>

Yeha, it does, I’ll link you to a simple codepen of what I have achieved, I think it works fine for what I wanted 🙂

> **[countdown](https://codepen.io/JackEdwardLyons/pen/NdPZme?editors=0010)**
>
> ...

---

<div class="post-metadata">

**Author:** ![ILM](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/ilm/32/363239_2.png) [@ILM](https://forum.freecodecamp.org/u/ILM)\
**Post date:** [January 16, 2021, 5:08pm UTC](https://forum.freecodecamp.org/t/add-a-specific-event-listener-based-on-button-text/73228/8 "2021-01-16T17:08:26Z")

</div>

Closed on January 16, 2021, 5:08pm UTC


