M.C.
M.C.

Reputation: 1745

Grabing Dojo Original Object given Dom Id

I have a DOJO Editor that I add in JS using the method createEditor

 require(["dijit/Editor"],
                function(Editor){
                    this.createEditor = function(idToReplace){
                        var myEditorA = new Editor({
                            height: '50px',
                            plugins:['bold','italic']
                        }, document.getElementById(idToReplace));
                        myEditorA.startup();
                    }
                });

I need the text inside the Editor after it has been changed.

I hooked up the method getEditorText but it is failing to do as I want it to do.

 require(["dijit/Editor"],  "dojo/dom",
    function(Editor, dom){
      this.getEditorText = function(idofEditor){
      //Editor myEditor =Editor(null, dom.byId(idofEditor)); does not work either

                    var myEditor = dom.byId(idofEditor);
                    var content = myEditor.get("value");
            });

The value that I need is stored in the attribute "value" in the Editor.

If I store myEditorA in a global variable I can get the content but I need the correct syntax to avoid working with unnecessary global variables

Upvotes: 1

Views: 269

Answers (1)

Dimitri Mestdagh
Dimitri Mestdagh

Reputation: 44675

In Dojo widgets (dijit) and DOM nodes are treated seperately. For DOM nodes you indeed need to use the dojo/dom module and the function byId(). For widgets however, you need the dijit/registry module and then you can get the widget by its DOM node like this:

require(["dijit/registry", "dojo/dom"], function(registry, dom) {
    var myEditor = registry.byNode(dom.byId(idofEditor));
});

But because the registry also saves your editor with the same ID as your DOM node, you can also access it directly (without using the DOM node) like this:

require(["dijit/registry"], function(registry) {
    var myEditor = registry.byId(idofEditor);
});

I made an example JSFiddle which you can find here.

Upvotes: 3

Related Questions