Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
153
rated 0 times [  157] [ 4]  / answers: 1 / hits: 61567  / 11 Years ago, wed, july 3, 2013, 12:00:00

I have a button in a modal window:



<button id=submit_btn class=btn btn-link>Send the invitation</button>


I'm trying to capture the click:



$('#submit_btn').click(function(event){
event.preventDefault();
alert( GO );
});


I've read that button clicks get swallowed in modal windows and that, to prevent that, use event.preventDefault(). But that's not working. I can't capture the click event of this button.



What else am I missing?



Any tips are greatly appreciated!


More From » jquery

 Answers
5

Try this -



$(document).on(click, #submit_btn, function(event){
alert( GO );
});


Or this -



$(document).delegate(#submit_btn, click, function(event){
alert( GO );
});


If you are using an older version of jQuery you may have to use the live method instead.



Live method (use this only if you have to)



$(#submit_btn).live(click, function(event){
alert( GO );
});


I'm fairly certain that one of these 3 methods above will solve your issue. The reason your event handler doesn't work is because your submit_btn element doesn't exist at the time your event handler is evaluated. The above 3 handlers I gave you will work on a submit_btn element that exists now and in the future.



/edit



Here is a jsfiddle that works - http://jsfiddle.net/GqD4f/8/



/another edit



I made a jsfiddle using the approach you had in your post and it works - http://jsfiddle.net/CR3bM/1/



Are you not putting your event handler in DOM ready?


[#77212] Wednesday, July 3, 2013, 11 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
briannar

Total Points: 354
Total Questions: 103
Total Answers: 101

Location: Japan
Member since Sat, Jun 6, 2020
4 Years ago
briannar questions
Tue, Aug 31, 21, 00:00, 3 Years ago
Sat, Jun 26, 21, 00:00, 3 Years ago
Sat, Jun 20, 20, 00:00, 4 Years ago
Tue, Apr 7, 20, 00:00, 4 Years ago
;