# How to get the ordinal of the current editor field from JavaScript?

**URL:** <https://forums.ankiweb.net/t/how-to-get-the-ordinal-of-the-current-editor-field-from-javascript/20195>\
**Category:** Development\
**Created:** [May 22, 2022, 11:12pm UTC](https://forums.ankiweb.net/t/how-to-get-the-ordinal-of-the-current-editor-field-from-javascript/20195 "2022-05-22T23:12:24Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![abdo](https://sea2.discourse-cdn.com/flex002/user_avatar/forums.ankiweb.net/abdo/32/654_2.png) [@abdo](https://forums.ankiweb.net/u/abdo)\
**Post date:** [May 22, 2022, 11:12pm UTC](https://forums.ankiweb.net/t/how-to-get-the-ordinal-of-the-current-editor-field-from-javascript/20195/1 "2022-05-22T23:12:24Z")

</div>

Anki used to set something like `id=f0` on the field divs. Currently, there doesn’t seem to be an obvious way to get the ordinal given a field’s shadow root obtained using `document.activeElement.shadowRoot`, for example.

---

<div class="post-metadata">

**Author:** ![hkr](https://avatars.discourse-cdn.com/v4/letter/h/c6cbf5/32.png) [@hkr](https://forums.ankiweb.net/u/hkr)\
**Post date:** [May 23, 2022, 12:31pm UTC](https://forums.ankiweb.net/t/how-to-get-the-ordinal-of-the-current-editor-field-from-javascript/20195/2 "2022-05-23T12:31:56Z")

</div>

It may not be an obvious way, but you can get the ordinal number of the field containing the shadow dom that has focus by iterating over the fields (the array of [EditorFieldAPI](https://github.com/ankitects/anki/blob/9b0729970ef25d7e99da24a08fc5cbc7d8c6556c/ts/editor/EditorField.svelte#L19)), as follows:

### (Typescript)

```javascript
async function getFieldOrdinal(target: Element): Promise<number | null> {
    let fieldOrdinal: number | null = null;
    const fields = require("anki/NoteEditor").instances[0].fields;
    for (const [index, fieldApi] of fields.entries()) {
        const field: HTMLElement = await fieldApi.element;
        if (field.contains(target)) {
            fieldOrdinal = index;
            break;
        }
    }
    return fieldOrdinal;
}

getFieldOrdinal(document.activeElement?.shadowRoot?.host!).then((ord) =>
    console.log(`ord: ${ord}`)
);

```

 ![field_ordinal](https://us1.discourse-cdn.com/flex002/uploads/anki2/original/2X/f/fdc0720d87c351e9467a9ce8b83dfabc36f6aba6.png)

---

<div class="post-metadata">

**Author:** ![abdo](https://sea2.discourse-cdn.com/flex002/user_avatar/forums.ankiweb.net/abdo/32/654_2.png) [@abdo](https://forums.ankiweb.net/u/abdo)\
**Post date:** [May 23, 2022, 5:14pm UTC](https://forums.ankiweb.net/t/how-to-get-the-ordinal-of-the-current-editor-field-from-javascript/20195/3 "2022-05-23T17:14:20Z")

</div>

> [@hkr](#):
>
> you can get the ordinal number of the field containing the shadow dom that has focus by iterating over the fields

I had something like this in mind, but couldn’t figure out how to do it. Thanks!

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex002/uploads/anki2/original/1X/8f1279ababc5879d54e4838989f606cfe55af8c7.jpeg) [@system](https://forums.ankiweb.net/u/system)\
**Post date:** [May 1, 2023, 3:15am UTC](https://forums.ankiweb.net/t/how-to-get-the-ordinal-of-the-current-editor-field-from-javascript/20195/4 "2023-05-01T03:15:32Z")

</div>

Closed on May 1, 2023, 3:15am UTC


