# Where to put event handler?

**URL:** <https://discuss.codemirror.net/t/where-to-put-event-handler/458>\
**Category:** Uncategorized\
**Created:** [September 8, 2015, 4:39pm UTC](https://discuss.codemirror.net/t/where-to-put-event-handler/458 "2015-09-08T16:39:29Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![NessBird](https://discuss.codemirror.net/user_avatar/discuss.codemirror.net/nessbird/32/137_2.png) [@NessBird](https://discuss.codemirror.net/u/NessBird)\
**Post date:** [September 8, 2015, 4:39pm UTC](https://discuss.codemirror.net/t/where-to-put-event-handler/458/1 "2015-09-08T16:39:29Z")

</div>

I’m a less experienced JS programmer, and I’m having a hard time filling in the blanks in the manual. I need to add an event handler:  
cm.on(“cursorActivity”, onCursorActivity);

This works fine when I add it into any context which contains a copy of cm, such as CodeMirror.defineOption(). But I don’t need to define an option. I’m using it for linting, so I want to put it in javascript-lint.js. CodeMirror.defineInitHook() looked good, but it doesn’t supply cm. It works when I add to lint.js, but I don’t think I’m supposed to mess with that one.

I’m sure I’m missing something obvious! Thanks for your help, and for a truly wonderful product!

---

<div class="post-metadata">

**Author:** ![marijn](https://discuss.codemirror.net/user_avatar/discuss.codemirror.net/marijn/32/136_2.png) [@marijn](https://discuss.codemirror.net/u/marijn)\
**Post date:** [September 9, 2015, 9:55am UTC](https://discuss.codemirror.net/t/where-to-put-event-handler/458/2 "2015-09-09T09:55:21Z")

</div>

At some point, you are creating an editor instance by calling `CodeMirror` or `CodeMirror.fromTextArea`. You can call `on` on the return value from that call. Or in an init hook, which most certainly are passed an editor instance as well.

---

<div class="post-metadata">

**Author:** ![NessBird](https://discuss.codemirror.net/user_avatar/discuss.codemirror.net/nessbird/32/137_2.png) [@NessBird](https://discuss.codemirror.net/u/NessBird)\
**Post date:** [September 9, 2015, 3:06pm UTC](https://discuss.codemirror.net/t/where-to-put-event-handler/458/3 "2015-09-09T15:06:51Z")

</div>

Oh, that’s what the editor instance is for 🙂 I think I get it. Thank you! That is working now.

And I’ve got

CodeMirror.defineInitHook(function(cm) {  
console.log(‘defining’);  
cm.on(“cursorActivity”, onCursorActivity);  
});

…Which also works when placed above my main CM setup call. So I’m on my way! Thanks again for answering my newbie questions 🙂
