# Only show editor button for specific notetype

**URL:** <https://forums.ankiweb.net/t/only-show-editor-button-for-specific-notetype/27257>\
**Category:** Development\
**Created:** [February 8, 2023, 11:35pm UTC](https://forums.ankiweb.net/t/only-show-editor-button-for-specific-notetype/27257 "2023-02-08T23:35:02Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Yannik](https://avatars.discourse-cdn.com/v4/letter/y/5fc32e/32.png) [@Yannik](https://forums.ankiweb.net/u/Yannik)\
**Post date:** [February 8, 2023, 11:35pm UTC](https://forums.ankiweb.net/t/only-show-editor-button-for-specific-notetype/27257/1 "2023-02-08T23:35:02Z")

</div>

I’m trying to make it so that my buttons will only show up if they can be used with the notetype of the note being loaded into the editor. However, the buttons are created before the note is loaded. So when the `editor_did_init_buttons` hook fires, the given `editor.note` is still null and I can’t check for the notetype.

```python
def add_audio(editor: Editor):
    (...)

def add_audio_btn(buttons: List[str], editor: Editor):
    if not has_fields(editor.note.note_type()): # <- doesnt work because its always null
        return
    buttons += [editor.addButton("", "add_audio", add_audio, "", "Add Audio")]

gui_hooks.editor_did_init_buttons.append(add_audio_btn)

```

Does anyone have an idea on how to solve this?

---

<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:** [February 12, 2023, 5:30pm UTC](https://forums.ankiweb.net/t/only-show-editor-button-for-specific-notetype/27257/2 "2023-02-12T17:30:13Z")

</div>

Try adding the button unconditionally in the `editor_did_init_buttons` hook, but hide it conditionally via JavaScript in the `add_cards_did_change_note_type` and `editor_did_load_note` hooks. Maybe try the CSS selector `button[title="Add Audio"]` to get the button in JavaScript and hide/show it.

---

<div class="post-metadata">

**Author:** ![Yannik](https://avatars.discourse-cdn.com/v4/letter/y/5fc32e/32.png) [@Yannik](https://forums.ankiweb.net/u/Yannik)\
**Post date:** [February 13, 2023, 8:14pm UTC](https://forums.ankiweb.net/t/only-show-editor-button-for-specific-notetype/27257/3 "2023-02-13T20:14:04Z")

</div>

Sounds like a good approach! I don’t know where/how I would be adding the JavaScript though… Could you give me a simple example?

---

<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:** [February 14, 2023, 9:31am UTC](https://forums.ankiweb.net/t/only-show-editor-button-for-specific-notetype/27257/4 "2023-02-14T09:31:27Z")

</div>

Here is an example adapted from an add-on I wrote that does something similar:

```python
from anki.models import NoteType
from aqt import dialogs, gui_hooks, mw
from aqt.addcards import AddCards
from aqt.editor import Editor

def update_button_visibility(editor: Editor, notetype: NoteType) -> None:
    if is_target_notetype(notetype):
        editor.web.eval("showAddonButton()")
    else:
        editor.web.eval("hideAddonButton()")

def on_notetype_changed(old: NoteType, new: NoteType) -> None:
    addcards: AddCards = dialogs.open("AddCards")
    update_button_visibility(addcards.editor, new)

def on_editor_did_load_note(editor: Editor) -> None:
    mw.progress.timer(
        50,
        lambda: update_button_visibility(editor, editor.note.note_type()),
        repeat=False,
    )

gui_hooks.add_cards_did_change_note_type.append(on_notetype_changed)
gui_hooks.editor_did_load_note.append(on_editor_did_load_note)

```

---

<div class="post-metadata">

**Author:** ![Yannik](https://avatars.discourse-cdn.com/v4/letter/y/5fc32e/32.png) [@Yannik](https://forums.ankiweb.net/u/Yannik)\
**Post date:** [February 15, 2023, 8:27pm UTC](https://forums.ankiweb.net/t/only-show-editor-button-for-specific-notetype/27257/5 "2023-02-15T20:27:27Z")

</div>

This works perfectly, thank you very much!

---

<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:** [March 17, 2023, 8:28pm UTC](https://forums.ankiweb.net/t/only-show-editor-button-for-specific-notetype/27257/6 "2023-03-17T20:28:04Z")

</div>

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.
