# Can't set attribute for an element

**URL:** <https://discuss.prosemirror.net/t/cant-set-attribute-for-an-element/5860>\
**Category:** Uncategorized\
**Created:** [September 18, 2023, 2:00pm UTC](https://discuss.prosemirror.net/t/cant-set-attribute-for-an-element/5860 "2023-09-18T14:00:50Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![shilpa](https://discuss.prosemirror.net/letter_avatar_proxy/v4/letter/s/a183cd/32.png) [@shilpa](https://discuss.prosemirror.net/u/shilpa)\
**Post date:** [September 18, 2023, 2:00pm UTC](https://discuss.prosemirror.net/t/cant-set-attribute-for-an-element/5860/1 "2023-09-18T14:00:50Z")

</div>

**In Javascript** we can set attribute like this ,

var myElement = document.getElementById(“myElementId”);

// Set the attribute

myElement.setAttribute(“attributeName”, “attributeValue”);

We would like to know, how to set an attribute for an element using prosemirror . Is there any code available to do the same thing mentioned above, using prosemirror.

**If we set the attribute using JavaScript it will remove after refresh by schema extension . So we need to add or update an attribute value for an element created using schema .**

---

<div class="post-metadata">

**Author:** ![sehyod](https://discuss.prosemirror.net/letter_avatar_proxy/v4/letter/s/3ab097/32.png) [@sehyod](https://discuss.prosemirror.net/u/sehyod)\
**Post date:** [September 18, 2023, 2:59pm UTC](https://discuss.prosemirror.net/t/cant-set-attribute-for-an-element/5860/2 "2023-09-18T14:59:09Z")

</div>

I think it depends on what you’re trying to achieve exactly.

You can use a plugin to add node decorations or inline decorations. You can take a look at the examples on prosemirror’s website, especially this one, that adds a class to elements: [ProseMirror lint example](https://prosemirror.net/examples/lint/)

Another solution would be to define a node with custom attributes and add the attributes to the DOM representation of the node. You can check this example: [ProseMirror dino example](https://prosemirror.net/examples/dino/)

From my understanding, the second solution seems to be the one you’re looking for, since you will then be able to change attributes in a transaction (with `tr.setNodeAttribute`)

---

<div class="post-metadata">

**Author:** ![shilpa](https://discuss.prosemirror.net/letter_avatar_proxy/v4/letter/s/a183cd/32.png) [@shilpa](https://discuss.prosemirror.net/u/shilpa)\
**Post date:** [September 19, 2023, 12:40pm UTC](https://discuss.prosemirror.net/t/cant-set-attribute-for-an-element/5860/3 "2023-09-19T12:40:41Z")

</div>

Thank You for your response.

I tried out given solutions . But i didn’t get the proper solution.

I will explain what i’m trying to do ,

1.Created nodes using schema , which contains unique id as an attribute .

1. These particular nodes are listed as a separate module of the project . The listed side will contain button named as ‘resolve’.

2. **While clicking on that button we need to add new attribute with a specific value to that particular element on editor.**

Could you please give me an example to do this…

---

<div class="post-metadata">

**Author:** ![shilpa](https://discuss.prosemirror.net/letter_avatar_proxy/v4/letter/s/a183cd/32.png) [@shilpa](https://discuss.prosemirror.net/u/shilpa)\
**Post date:** [September 27, 2023, 11:04am UTC](https://discuss.prosemirror.net/t/cant-set-attribute-for-an-element/5860/6 "2023-09-27T11:04:29Z")

</div>

Issue resolved 😀
