Updating attrs of a given node


Hi everyone,

Is it possible to update attrs of a given node without implementing custom NodeView?

Basically, I need a function like updateAttrs(node, attrs){ … }

I was looking at: tr.setNodeMarkup(pos, undefined, myattrs);

…but I can’t find how to determine “pos”.

Also, I tried to use

setBlockType(node.type, { myattrs })(state,dispatch);

…but updates are ignored.

I would appreciate any help.


If you don’t know a node’s position, you can’t interact with it—node objects can’t be acted on by identity. You can, in fact, have the same node object in your document multiple times, and different node objects with the same content are treated as interchangeable. Thus, the only way to address these is by document position, and you’ll have to find some way to get a position for the node that you want to change.

setBlockType is a command that’ll act on the current selection—probably not what you want here.


Hi Marijn, thanks for quick reply.

This is actually what I’m having troubles with: I don’t know how to find position of element under cursor. So, I’d appreciate any options and/or guidance.

Also, I’m not sure I understand about “same node object multiple times”. Could you please elaborate?


If you mean text cursor, you might want to look into state.selection.$head. If you mean mouse cursor, try view.posAtCoords.

This works:

let paragraph = schema.node("paragraph")
let doc = schema.node("doc", null, [paragraph, paragraph])