pvitt
pvitt

Reputation: 1065

dom.byId returns undefined after the element has been added

Ive got a function that is adding a div as a child to a dijit/layout/conentpane. The problem I'm having is that after I add this div I can't reference it by using dom.byId: The function that creates this div is called from a click event of a menuitem:

the menu item:

<div id="miLMWS" data-dojo-type="dijit/MenuItem" data-dojo-props="onClick:function(){SetPage('LMWS');}">Watershed</div>

the onclick function (SetPage):

....

function (dom, parser, registry, ready, WatershedMap, OWOWMapGallery) {
            window.SetPage = function (pg) {
                //set the page depending on the menu item clicked
                ClearWidget();//destroys previous widget deleting the div
                AddToolDiv();//add the div
                switch (pg) {
                    case 'LMWS':
                        var test = dom.byId("tool"); //this is undefined
                        //WS = new WatershedMap({}, "tool");
                        break;
                }
            };
...

The addDivFunction:

function AddToolDiv() {
     //add page div (node where template will be placed in
     var tool = document.createElement("div");
     tool.setAttribute("id", "tool");
     var ContentContainer = registry.byId("MainContent");
      ContentContainer.set("MainContent", tool);
      var test = dom.byId("tool");//this is undefined
 }

Each time a menu item is clicked a new custom widget gets loaded into the tool div. (There is a function ClearWidget that destroys the old widget and the tool div) I'm thinking that maybe this has something to do with calling the function from the onClick event? When I call the AddToolDiv when the page first loads the test variable correctly refers to the tool div. Any help would be most appreciated.

Thanks

Upvotes: 0

Views: 866

Answers (2)

Frode
Frode

Reputation: 5710

The byId function will search the DOM for an element with the given id. If your tool element hasn't actually been inserted into the DOM when you call byId, you will get undefined.

The code you have shown never actually inserts the tool element into the DOM, it only passes it to the ContentContainer.set("MainContent", tool) call. Be sure to check that this call actually inserts the tool element into the DOM immediately. Is ContentConainer itself inserted into the DOM when you call byId?

Upvotes: 1

mannyyysh
mannyyysh

Reputation: 339

you can try these things

var test = dom.byId("tool",ContentContainer.domNode);

else you can hold the reference of this dom directly this.tool = document.createElement("div");

else use domconstruct api provided in dojo

Upvotes: 0

Related Questions