# Set property of all table cells

**URL:** <https://discuss.prosemirror.net/t/set-property-of-all-table-cells/4591>\
**Category:** Uncategorized\
**Created:** [May 2, 2022, 7:14am UTC](https://discuss.prosemirror.net/t/set-property-of-all-table-cells/4591 "2022-05-02T07:14:40Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![AlfMagne](https://discuss.prosemirror.net/user_avatar/discuss.prosemirror.net/alfmagne/32/2804_2.png) [@AlfMagne](https://discuss.prosemirror.net/u/AlfMagne)\
**Post date:** [May 2, 2022, 7:14am UTC](https://discuss.prosemirror.net/t/set-property-of-all-table-cells/4591/1 "2022-05-02T07:14:40Z")

</div>

Hi

In our work, we have started to implement the prosemirror editor. In it, we added support for tables, and have made use of cell attributes, example from schema

```auto
cellAttributes: {
    background: {
        default: null,
        getFromDOM(dom) {
            return (dom.dataset && dom.dataset.cellBackground) || null;
        },
        setDOMAttr(value, attrs) {
            if (value){
                attrs["data-cell-background"] = value
            }
        }
    }
}

```

We have also implemented our own menubar. From the menu bar, we do the following to update background colour of table cells:

```auto
        const cmd = setCellAttr("background", colour)
        cmd(this.state, this.dispatch)

```

This works great. However, I’m having some problems figuring out how to

1. On click on a button
2. Iterate all table cells
3. clear background attribute on iterated table cell

i.e. walk through all table cells, and call `setCellAttr("background", null)`

This is something we need when we want to “remove all styling” from the editor.

Thanks in advance

---

<div class="post-metadata">

**Author:** ![marijn](https://discuss.prosemirror.net/user_avatar/discuss.prosemirror.net/marijn/32/15_2.png) [@marijn](https://discuss.prosemirror.net/u/marijn)\
**Post date:** [May 2, 2022, 8:17am UTC](https://discuss.prosemirror.net/t/set-property-of-all-table-cells/4591/2 "2022-05-02T08:17:47Z")

</div>

You could use use [`state.doc.descendants`](https://prosemirror.net/docs/ref/#model.Node.descendants), check whether each node is a table cell with attributes to clear, and if so, use [`setNodeMarkup`](https://prosemirror.net/docs/ref/#transform.Transform.setNodeMarkup) to build up a transaction that clears the attribute.

---

<div class="post-metadata">

**Author:** ![AlfMagne](https://discuss.prosemirror.net/user_avatar/discuss.prosemirror.net/alfmagne/32/2804_2.png) [@AlfMagne](https://discuss.prosemirror.net/u/AlfMagne)\
**Post date:** [May 2, 2022, 8:51am UTC](https://discuss.prosemirror.net/t/set-property-of-all-table-cells/4591/3 "2022-05-02T08:51:40Z")

</div>

Thank you so much for the quick reply

Is there a straightforward way of getting the position of each cell, i.e. for the first argument to setNodeMarkup

```nohighlight
this.doc.descendants((node) => {
    if (node.type.name === "table_cell" && node.attrs.background) {
        // const pos =
        this.state.tr.setNodeMarkup(pos, null, setAttr(node.attrs, "background", null))
    }
})

```

---

<div class="post-metadata">

**Author:** ![marijn](https://discuss.prosemirror.net/user_avatar/discuss.prosemirror.net/marijn/32/15_2.png) [@marijn](https://discuss.prosemirror.net/u/marijn)\
**Post date:** [May 2, 2022, 9:17am UTC](https://discuss.prosemirror.net/t/set-property-of-all-table-cells/4591/4 "2022-05-02T09:17:57Z")

</div>

> [@AlfMagne](#):
>
> Is there a straightforward way of getting the position of each cell,

Yes, it’s the second argument to the callback you pass to `descendants`.

---

<div class="post-metadata">

**Author:** ![AlfMagne](https://discuss.prosemirror.net/user_avatar/discuss.prosemirror.net/alfmagne/32/2804_2.png) [@AlfMagne](https://discuss.prosemirror.net/u/AlfMagne)\
**Post date:** [May 2, 2022, 9:30am UTC](https://discuss.prosemirror.net/t/set-property-of-all-table-cells/4591/5 "2022-05-02T09:30:07Z")

</div>

Fantastic 🙂

This works:

```auto
this.doc.descendants((node, pos) => {
    if (node.type.name === "table_cell" && node.attrs.background) {
        tr.setNodeMarkup(pos, null, setAttr(node.attrs, "background", null))
    }
})

```
