views:

129

answers:

2

I have a code the change the html of a div to make a button. When I make a click handler for the dynamic button, nothing happens

$('#signinup').html("<button id=\"login_submit\">Sign In</button>");

And the handler:

$('#login_submit').click(function() {
   alert("Works!");
});
+1  A: 

When does the click handler code run in conjunction with the HTML generation? Also, have you tried the bind or live event handler methods?

Brian
+2  A: 

See the working demo :)

Use the live() method:

$('#login_submit').live('click', function() {
   alert("Works!");
});

The live() method attach a handler to the event for all elements which match the current selector, now or in the future.

Sarfraz
The demo does work as advertised, though it doesn't use `live`...
joshperry
@joshperry: Thanks for notifying that, actually I posted a wrong working link, updated now. Thanks
Sarfraz
Thank you, accepted.
Neb
@Neb: You are welcome....
Sarfraz
Just what i needed, many thanks :)
Kohan