Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7658253
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T13:10:40+00:00 2026-05-31T13:10:40+00:00

Is it better to have multiple handlers for button click events, or is it

  • 0

Is it better to have multiple handlers for button click events, or is it suitable to combine click handlers with a switch statement? This works in my code, but I was wondering, is there a cooler, more JQuery-istic way to do it?

<html>
    <button id="button1">Button 1</button>
    <button id="button2">Button 2</button>
    <button id="button3">Button 3</button>
</html>

Which is better, this?

$('button').click(function(){
    switch(this.id){
        case "button1": 
            alert("Do what button 1 says");
            break;

        case "button2":
            alert("Do what button 2 says");
            break;

        case "button3":
            alert("Do what button 3 says");
            break;
    }
});

Or this,

$('#button1').click(function(){
    alert("Do what button 1 says")
});

$('#button2').click(function(){
    alert("Do what button 3 says")
});

$('#button3').click(function(){
    alert("Do what button 3 says")
});

…Or is there no difference?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T13:10:41+00:00Added an answer on May 31, 2026 at 1:10 pm

    It all depends on the event handler. If there is shared code between the handlers and only a small part is specific, it might be fine to distinguish between the buttons inside the handler.
    If each button handler is totally different though, why put three unrelated functions into one?

    You can also create a map of functions:

    var handlers = {
        id1: function() {
            // ...
        },
        id2: function() {
            //...
        }
    };
    

    and simply call a function with handlers[this.id]().

    In the end, you assign an even handler to each element one way or the other, so it’s more a matter of how you want to structure your code (which is personal preference more or less).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm having troubles releasing objects.. To explain it better I have included my code
This post is related to: POSIX TIMER- Have multiple timers I want to call
On my web page, I have multiple independent plug-ins (for lack of a better
I have jQuery code that handles unbinding and binding event handlers to elements that
I have a website with multiple pages, each of them with custom event handlers
I have this task of designing a new interface for a distributed application (multiple
Here we go again, the old argument still arises... Would we better have a
Is it better to have a different CSS file for each user-agent or is
I have a team of junior developers and they want to have better understanding
For example is it better to have: public class Case : EntityIdentifiable { public

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.