Can't set attribute for an element

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 .

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

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

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)

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…

1 Like

Issue resolved :grinning: