Select to view content in your preferred language

How to call module function on button click in arcgis javascript

2373
2
05-19-2015 06:07 AM
SadanandacharB1
New Contributor III

I want to call module function on button click in arcgis javascript, example,

dojo.connect(dom.byId("btnLocate"), "click", function ()

          {

            example();

          

            

          });

function example()

      {

          alert("df");

       myModule.queryStatesLayer();

      }

<button style="height: 26px; width: 76px; margin-top: 22px;" name="btnLocate" onclick="example();">Locate</button>

here 'alert' is working but i can't access module function, how to call module function on button click? Please guide me with an example

0 Kudos
2 Replies
ChrisSmith7
Frequent Contributor

I am sure there is a better answer for this as whenever someone mentions "global vars", you should generally think "no" to avoid collisions and conflicts - however, I have accomplished this via the following:

//instantiate your module
window.objmyModule = new myModule({
     "myParam": paramValue
});

//call a module function after you've instantiated the object
objmyModule.myFunction();
thejuskambi
Regular Contributor

You wont be able to call the module function unless your module is part of global variables.

one solution is you can change the context of the function by using lang.hitch like below


dom.byId("myButton").on("click", lang.hitch(myModule, function(){

     this.example();

});

You can pass "this" instead of myModule, if the event initialization and method are in the module.