How to get a button in the Openlayers popup?

I am trying to place a button inside the Openlayers popup. As long as the button is displayed correctly, with the following code, the "handlerFunc" function is not executed when the button is pressed. The code segment that I posted is within the same function (therefore handlerFunc is actually a nested function). I use jQuery for the button itself. Any ideas on what could go wrong? Thanks!

var feature = new OpenLayers.Feature(presences, ll); feature.popupClass = popupClass; feature.data.popupContentHTML = "<button id='popupButton'>Click me</button>"; feature.data.overflow = (overflow) ? "auto" : "hidden"; feature.data.icon = markerIcon; $('#popupButton').button(); $('#popupButton').click(handlerFunc); function handlerFunc() { // do something } 
+4
source share
1 answer

Most likely, the reason is that your button does not exist when you snap to the click event. $('#popupButton') returns null . Instead of using $('#popupButton').click(handlerFunc); try $('#popupButton').live('click', handlerFunc); . This means that we are attached to the event not only when the DOM is created, but also when the object appears.

+3
source

All Articles