Search code examples
javascripthtmlace-editor

Ace Editor - How to find one or all instances?


I try to get and set the value of an ace editor on a random website. But I do not know the instance names. Also there is no ID on any div. Is there a way to find one or all instances, so that I can access the api like:

var editor = ace.edit("...");

Thanks for your advice :)


Solution

  • all ace editors need to have ace_editor classname and ace.edit saves editor to <domNode>.env.editor so the following should work in most cases

    var all = document.querySelectorAll(".ace_editor");
    for (var i = 0; i < all.length; i++) {
        if (all[i].env && all[i].env.editor)
            console.log(all[i].env.editor)
        else
            console.log("can't get editor from" all[i])
    }
    

    This won't work for editors created as new Editor() which do not have envand for editors created in shadow dom, which can't be found via querySelectorAll.