I'm developing an extension (my first extension) for google Chrome.
I have a problem, if I write html code for button directly in popup.html it works but if I use javascript to create that html/button that button doesn't works. I didn't get any error from my console.
Button appears in html but if I click on it any event is launched.. Code button is the same.
Example
this code:
<button type="button" id="button1">Connect</button>
If I paste this code directly in popup.html works but if is create with
for (i in dataservers.servers) {
var serverNumberList = "button"+[i];
x += "<button type='button' id='"+ serverNumberList +"'>Connect</button>";
document.getElementById("viewServers").innerHTML = x;
}
doesn't works..
Button call:
$("#button1").click(function(){
window.alert('GO!');
});
In this screenshot you can look that the code is the same. The first one works the second on doesn't works. Some idea?
SOLVED
in my comment I found:
When you write $("#button1") the elements must exist at that time, but they don't in your case AFAICT, so if my guess is right you need to use event delegation. – wOxxOm
I solved with this code:
$(document).on('click', '#button1', function(evt) {
window.alert('GO!');
});
Thank you guys!