Fonzy
Fonzy

Reputation: 201

JQuery Mobile Dynamic Button add Click

I have an infowindow in a google map like so,

var content = '<div id="link"><input type="button" value="Report this light" id="reportBtn"/></div>';

i am using jquery mobile to bind a 'click' event when the infowindow pops open on the map but it doens't fire, my code:

$(document).on('pageinit', function() {
 $('#reportBtn').on('click', function() {
     alert('it works');
 });
});

Upvotes: 0

Views: 1269

Answers (1)

peterm
peterm

Reputation: 92785

You need to use event delegation. Try

$(document).on('pageinit', function() {
    $(document).on('click', '#reportBtn', function() {
        alert('it works');
    });
});

Instead of document you can use nearest static element that is a parent to <div id="link">.

$('#nearestparent').on('click', '#reportBtn', function() {...});

Upvotes: 2

Related Questions