wiring a button click event in an InfoWindow

3293
3
06-05-2015 08:53 AM
JosephRogan
New Contributor III

Hi, I have InfoWindow popups when users clicks a layer on my map.  I am doing a lot of custom formating in these info windows and require having a html button in them.  I can get the button to appear no trouble, but I can't get it's click event to work or call a function in my code.

I've tried multiple approaches including using:

<button id='myButton' onclick='myFunction();'>Label</button>

when I create the button in the InfoTemplate.setContent method.  Doing this tells my that my function is undefined.

Also tried wiring the event in multiple location such as by using:

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

     console.log("test");

});

But nothing happens.

Thanks for the help!

0 Kudos
3 Replies
JosephRogan
New Contributor III

I sorta just came up with a workaround, right after making the post.

when using the setContent method, at the end of the content I have the string:

{$OBJECTID:myFunction}

Then, within myFunction I can wire the click event with my button id to an anonymous function that prints test to the console.

Seems rediculous but it works.  Still don't get why my 2nd example in the original question doesn't just work.

0 Kudos
RickeyFight
MVP Regular Contributor

Joseph,

I know this code is not quite what you are looking for, in this script a html link is added and performs a function when clicked.

Data Grid Related Records Zoom

I hope this helps

KristianEkenes
Esri Regular Contributor

With the <button id='myButton' onclick='myFunction();'>Label</button> approach, how are you defining the function?

function myFunction(){
  //this will probably be undefined
}

Instead try:

window.myFunction = function(){
  //this may work
}
0 Kudos