# OnEvent within another onEvent allowed?

**URL:** <https://forum.code.org/t/onevent-within-another-onevent-allowed/15025>\
**Category:** Unit and Lesson Discussion\
**Tags:** csp-unit-5, csp-unit-5-lesson-5\
**Created:** [March 15, 2018, 8:22pm UTC](https://forum.code.org/t/onevent-within-another-onevent-allowed/15025 "2018-03-15T20:22:34Z")\
**Posts on this page:** 1\
**Showing post:** 2

<div class="post-metadata">

**Author:** ![josh](https://sea2.discourse-cdn.com/flex016/user_avatar/forum.code.org/josh/32/868_2.png) [@josh](https://forum.code.org/u/josh)\
**Post date:** [March 15, 2018, 9:44pm UTC](https://forum.code.org/t/onevent-within-another-onevent-allowed/15025/2 "2018-03-15T21:44:49Z")

</div>

Oooh, this is a tricky misconception about how event handlers work. When you call `onEvent()` it creates a new event handler sitting in the background responding to events. Once you’ve created an event handler, there’s no way to get rid of it. When you restart the game the old event handlers are still hanging out in the background _and_ new event handlers are created, which is why you see it changing by 2 instead of one.

As a rule, you should never put an `onEvent()` block inside another block in order to prevent duplicating your event handlers. One approach to getting the kind of behavior your student is looking for would be to create a single event handler at the top level for each of “dogclicker” and “catclicker” and then use a variable to track whether you have chosen dog or cat mode. This could roughly look like this:

```javascript
var target = "dogclicker";

onEvent("dogclicker", "click", function() {
  if (target == "dogclicker") {
    score = score + 1;
  } else {
    lives = lives - 1;
  }
}

onEvent("catclicker", "click", function() {
  if (target == "catclicker") {
    score = score + 1;
  } else {
    lives = lives - 1;
  }
}

```

---

_[View the full topic](https://forum.code.org/t/onevent-within-another-onevent-allowed/15025)._
