tags:

views:

48

answers:

6
+3  Q: 

jquery click event

can we use more than one click event in the Jquery as

$(document).ready(function(){
$('#button').click(function(){
...........click(function(){
...........click(function(){
});

$(this).hide();
});
}); 

pls help me

i wrap the code due to less space for the code

$('#togglebutton1').click(function() {
      if ($(this).is(':visible')){ 
          $('#bio>div,#heading2,#heading3 ').hide(); 
          $('#bio>div:first').show();
          $('p:first').toggle(
             function() { 
                 $(this).animate({ 'height': '+=15px' }, '1000', 'linear'); 
             }, 
             function() {
                 $(this).animate({ 'height': '-=15px' }, '1000', 'swing');
             }
          );
          $(this).val('ShowImage');
       }else { 
         $(' #bio > div,#heading2,#heading3 ').show(); 
         $(this).val('HideImage'); 
       } 
}); 
$('#bio h3').click(function() { 
     $(this).next().animate({ 'height': 'toggle' }, 'slow', 'easeOutBounce'); }); 
}); 
A: 

Yes you can bind more than one click event. Have a look at the jQuery "bind" method.

http://api.jquery.com/bind/

Mark Redman
A: 

You can use the bind for that.

Example:

$('#foo').bind('click mouseenter mouseleave', function() {
  $(this).toggleClass('entered');
});

As can be seen, three evens are attached eg click mouseenter mouseleave to the element with id set to #foo.

Sarfraz
*"More than one click event"*, not more than one event.
T.J. Crowder
@Crowder: `click` is also an event, right? Isn't `bind` used for multiple events?
Web Logic
@Web Logic: Yes, it is, and that's cool. It just doesn't answer the question the OP had (at least, as far as I can tell what the question is).
T.J. Crowder
what's the point of multiple click, it will boil down to same things. The OP basically means multiple events.
Sarfraz
@Sarfraz: See my response to your identical comment on my answer.
T.J. Crowder
@T.J. Crowder: You are right, but i still suspect, OP basically means more than one event, let's see what he has to say :)
Sarfraz
A: 

yes you can... jQuery is good on chaining...

but I think you should clarify more on your question on what really you want to achieve... so that we can suggest more and better idea.. cheers!

Reigel
+2  A: 

Yes, you can bind multiple handlers to the click event of the same element:

$('#myElementID').click(function() {
    // do something here
})

// Elsewhere (presumably)
$('#myElementID').click(function() {
    // do something else here
})

Provided the first handler that gets called doesn't stop the event (via .stopPropagation() or by returning false from the handler), the next one will get called. jQuery guarantees that handlers will get called in the order in which they're registered (even when the browser doesn't). (This is covered in the bind docs, but applies to the click function as well, as click is just a shorthand version of bind for the click event.)

T.J. Crowder
what's the point of multiple click, it will boil down to same things. The OP basically means multiple events.
Sarfraz
@Sarfraz: Usually this comes up when you have disparate parts of the code that both need to hook the event, which either don't know about each other or which shouldn't be inter-dependent. It's the whole point of DOM2 handlers (the `addEventListener`/`attachEvent` browsers provide that jQuery uses under the covers). The previous "DOM0" mechanism, `onclick` attributes, only allowed a single handler, which was limiting. Re what the OP means, that's for them to clarify. What they've *said* is "multiple click events".
T.J. Crowder
+2  A: 

I think you may have meant running the same function on several different elements. This can be done as such:

$("#mydiv1, #mydiv2, #mydiv3").click(function(){
    //some code
});

Good luck!

yuval
A: 
$(function(){
    $('#link').click(function(e){
        e.preventDefault(); // unbind default click event
        function1();
        function2();
        function3();
        // or
        var $this = $(this);
        $this.function1();
        $this.function2();
        $this.function3();
    });
});
Jenechka