trigger button click event automatically using jQuery

by Developer 4. February 2010 14:21

There are some situations where you have to execute some events automatically like you have to raise button click event without any action on button.

 

There is jQuery trigger() method to execute any event automatically in javascript.

 

Syntax of jQuery trigger() method:

                                                              trigger(eventType); //where eventType is type of event like click event of button

 

Here I am explaining jQuery trigger()  method to execute click event of HTML button.

 

<html>
    <head>
        <title> execute button click event automatically using jQuery trigger() method</title>
        <script type="text/javascript" src="jquery.js"></script>
        <script type="text/javascript">
            $(document).ready(function() {          
                $('#btn1').bind('click', function() {
                    alert('You selected the First Button');
                });
                $('#btn2').bind('click', function() {
                    alert('You selected the Second Button');
                });


                var d = new Date();
                if(d.getSeconds() % 2 == 0){
                    $('#btn1').trigger('click');
                }              
                else{
                    $('#btn2').trigger('click');
                }
            });   
        </script>
    </head>
    <body>
        <input type="button" id="btn1" value="First Button" />
        <input type="button" id="btn2" value="Second Button" />     
    </body>
 </html>

 

In the above code, we have two HTML buttons btn1, btn2. We bind the click event for two buttons using bind() jQuery method.

 

                var d = new Date();
                     if(d.getSeconds() % 2 == 0){
                        $('#btn1').trigger('click');
                      }              
                     else{
                        $('#btn2').trigger('click');
                    }

 

Here we are raising the click events for two buttons alternatively. $('#btn1').trigger('click'); attaches the click event to the button btn1 and $('#btn2').trigger('click'); attaches the click event to the button btn2. So, jQuery trigger() method will raise click events for two buttons automatically.

If the datetime second value is even(i.e second%2 == 0) we are raising the btn1 click event and for odd we are raising the btn2 click event automatically.

 

Download source code: jQuerey_trigger.zip (35.79 kb)

Comments

5/26/2010 10:23:20 AM #

watch full movies

We are proud to announce that we open the gates to our fast growing online entertaining community that have a bright future and new ideas. On www.total-request.com you can find high quality movies, TV Shows, Serials, News about Movies and Actors and much more, totally free of charge. Visit our total-request.com  - database!!!

watch full movies

5/30/2010 12:34:44 AM #

Maynard Sosby

Of course, what a great site and informative posts, I will add backlink - bookmark this site? Regards, Fitness maniac.

Maynard Sosby

6/7/2010 11:56:46 PM #

Alica Macklem

Moe's Tavern Prank Call Generator! HILARIOUS!!!

Alica Macklem

6/21/2010 10:55:31 PM #

Gabriel Happe

Damn, awesome website. I actually came across this on Google, and I am really happy I did. I will definately be coming back here more often. Wish I could add to the conversation and bring a bit more to the table, but am just taking in as much info as I can at the moment.

Thank You

<A href="http://letswatchmoviesonline.com">watch movies online</A>

Gabriel Happe

6/28/2010 4:21:08 PM #

abercrombie

I be able to not approve on all, but the topic is really attractive. Congratulations!

abercrombie

7/8/2010 1:44:59 PM #

Top 10 interview questions

Hey site owner, I like MS Blog Engine but why did you chose it?  I mean it's so cool with the random avatars and draws people in, but what speficially made you want to use it?

Top 10 interview questions

7/9/2010 6:26:20 AM #

male enhancement

There's nothing far better when compared with performing at your peak. I love it, along with so does my girl.

male enhancement

7/14/2010 12:51:49 AM #

Elana Fremon

Hello. That is kind of an "unconventional" question , however produce other visitors requested you the way get the menu bar to seem like you've got it? I also have a weblog and am really trying to alter across the theme, however am scared to loss of life to mess with it for concern of the search engines punishing me. I'm very new to all of this ...so i am just not optimistic exactly how one can attempt to to all of it yet. I am going to simply hold working on it in the future at a time Thanks for any assist you may supply here.

Elana Fremon

Add comment




  Country flag

Click to change captcha
biuquote
  • Comment
  • Preview
Loading



Page List

    Tag cloud