jQuery Trigger example
Blogs20112011-10-06
jQuery Trigger example
Suppose we have a popup window in a webpage. In the popup window, there is a ‘link’ and a ‘input button’ to do the same thing: close the window, like this:
<a href="#" class="modal-close"></a>
// and:
<input type="button" value="Close" />We can use jQuery ’trigger’ to simplify the codes:
//1. for the first, define the event.
$('.modal-close').click(function(){
$('.modal-window').fadeOut(200, function(){
$('#modal-overlay').hide();
});
return false;
});
//2. for the second, just trigger the first event.
$("input[value=Close]").click(function() {
$('.modal-close').trigger('click');
});This make the codes simple and easy to maintain.
Use ‘input:checkbox’ instead of ‘:checkbox’
Sometimes we use the following to process chechbox:
$(“:checkbox”).serialize();
A better option is:
$(“input:checkbox”).serialize();
:checkbox is the same as *:checkbox and this means jQuery will execute “checkbox” == emlement.type for EVERY element in the DOM - pretty slow stuff.
