William Jiang

JavaScript,PHP,Node,Perl,LAMP Web Developer – http://williamjxj.com; https://github.com/williamjxj?tab=repositories

jQuery Trigger example

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.

Advertisements

One response to “jQuery Trigger example

  1. powercashadvance.com 10/26/2011 at 3:00 pm

    Thank you for your time and energy to have had these things together on this blog site. John and that i very much loved your suggestions through the articles with certain things. I am aware that you have a variety of demands on schedule hence the fact that a person like you took all the time just like you did to guide people like us by this article is also highly appreciated.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: