Heres my link:
If you click on the link in test.php, it opens in a modal box which is using the jquery ‘facebox’ script.
I’m trying to act upon a click event in this box, and if you view source of test.php you’ll see where I’m trying to loacte the link within the modal box.
$('#facebox .hero-link').click(alert('click!'));
However, it doesn’t detect a click and oddly enough the click event runs when the page loads.
The close button DOES however have a click event built in that closes the box, and I suspect my home-grown click event is being prevented somehow, but I can’t figure it out.
Can anyone help? Typically its the very last part of a project and its holding me up, as is always the way 😉
First, the reason you’re getting the alert on document load is because the
#clickmethod takes a function as an argument. Instead, you passed it the return value ofalert, which immediately shows the alert dialog and returns null.The reason the event binding isn’t working is because at the time of document load,
#facebox .hero-linkdoes not yet exist. I think you have two options that will help you fix this.Option 1) Bind the click event only after the facebox is revealed. Something like:
Option 2) Look into using the jQuery Live Query Plugin
jQuery Live Query will automatically bind the click event when it recognizes that Facebox modified the DOM. You should then only need to write this: