# How to add className to specified text and bind a click event?

**URL:** https://discuss.codemirror.net/t/how-to-add-classname-to-specified-text-and-bind-a-click-event/9602
**Category:** v5
**Created:** [December 5, 2025, 8:45am UTC](https://discuss.codemirror.net/t/how-to-add-classname-to-specified-text-and-bind-a-click-event/9602 "2025-12-05T08:45:19Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![luorenfei](https://discuss.codemirror.net/letter_avatar_proxy/v4/letter/l/9e8a1a/32.png) [@luorenfei](https://discuss.codemirror.net/u/luorenfei)
#### Post date: [December 5, 2025, 8:45am UTC](https://discuss.codemirror.net/t/how-to-add-classname-to-specified-text-and-bind-a-click-event/9602/1 "2025-12-05T08:45:20Z")

</div>

As the title suggests, I’m trying to get the corresponding text from the DOM and replace it, adding the className and click event I need, but it seems to be forcibly reset? Is there another way to achieve this?

 ![r4_312212363263022144](https://discuss.codemirror.net/uploads/default/original/2X/4/4f60f26a2515619117c437d8200f7a36651b46c4.png)

---

<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: [December 5, 2025, 8:49am UTC](https://discuss.codemirror.net/t/how-to-add-classname-to-specified-text-and-bind-a-click-event/9602/2 "2025-12-05T08:49:07Z")

</div>

The DOM in the editor is controlled by the library, because it needs to be able to draw and update the document, so you cannot just mutate it. See [the decoration example](https://codemirror.net/examples/decoration/) for information on how to add attributes around content. Don’t put event handlers directly on nodes, just use [an editor-wide handler](https://codemirror.net/docs/ref/#view.EditorView%5EdomEventHandlers) and check the event target.
