Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    Regular Coder
    Join Date
    Oct 2002
    Posts
    147
    Thanks
    31
    Thanked 2 Times in 2 Posts

    multiple ids to share one click event

    Is there a way to assign multiple ids to share one click event?


    Code:
    //this is what i'm doing now - to much code
    $("#button1").click(function(){
      alert('test');
    });
    $("#button2").click(function(){
      alert('test');
    });
    $("#button3").click(function(){
      alert('test');
    });
    $("#button3").click(function(){
      alert('test');
    });
    Code:
    //is this possible
    $("#button1"   or "#button2" or "#button3" or "#button4" ).click(function(){
      alert('test');
    });

  • #2
    Senior Coder
    Join Date
    Feb 2009
    Location
    Ilkley, West Yorkshire, UK
    Posts
    2,950
    Thanks
    9
    Thanked 724 Times in 718 Posts
    Yes, separate your selectors with commas:

    Code:
    $("#button1,#button2,#button3,#button4" ).click(function(){
      alert('test');
    });


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •