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 7711417
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T01:22:12+00:00 2026-06-01T01:22:12+00:00

I have a jQueryMobile site/app where performance is an issue. I am reading a

  • 0

I have a jQueryMobile site/app where performance is an issue. I am reading a lot about how it’s smart not to bind too many separate handlers. So if you have a page with a lot of buttons , the advice is to bind like this…

$(document).on('click',function(){
   if (this = $('#button1')){
       //Handler for button one
   } else if (this = $('#button2') {
       //etc
   } else //etc
})

…rather than like this…

$('#button1').click(function(){
    //Handler for button one
})
$('#button2').click(function(){
    //Handler for button two
})
//etc

So I am wondering to what extent this is truly useful.

What if I made one listener for all ‘pageshow’ events in my document? (a jQueryMobile website is a single document containing many pages, and many of those pages need a bit of dolling up around showtime).

What if I made one click listener for all forty-odd buttons in the document, and used the handler to distinguish which button we’re dealing with and what needs to be done in response?

  • 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-06-01T01:22:14+00:00Added an answer on June 1, 2026 at 1:22 am

    Your first option is faster to install and initialize, but slower to run when an event happens.

    If you had hundreds of buttons with all nearly identical event handling, it might make sense to only install one event handler for all of them rather than run the code to install hundreds of event handlers. But, you would still pay a small performance penalty when a particular event happens.

    If you want to maximally optimize for run-time performance at the time of an event, it is still faster to install an event handler on the specific object you want to monitor. That’s because the event handlers are kept in a separate list just for that object and the code doesn’t have to sort through a long list of event handlers to find which one is appropriate for that specific object and event.

    Also, your if statement in the first option will never work for several reasons. It would need to be something like this:

    $(document).on('click',function(e) {
       if (e.target.id == 'button1') {
           //Handler for button one
       } else if (e.target.id == 'button2') {
           //etc
       } 
    })
    

    And, anytime you use delegated event handling, it’s better to pick a common parent for the buttons that is much closer to the actual objects than the document object (helps runtime performance).

    $("#buttonContainer").on('click',function(e) {
       if (e.target.id == 'button1') {
           //Handler for button one
       } else if (e.target.id == 'button2') {
           //etc
       } 
    })
    

    If you wanted just button clicks with delegated event handling, you could use some of the delgation features built into .on() like this

    $("#buttonContainer").on('click', 'button', function(e) {
       if (this.id == 'button1') {
           //Handler for button one
       } else if (this.id == 'button2') {
           //etc
       } 
    })
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a scaling problem in a PhoneGap app. My issue is very similar
i have a problem with jQuery and jQueryMobile. I have a site with 2
I have already spent a lot of time on Google, but have not found
I have created a small app having navigation UI similar to jQuery Mobile site.
I have a jquerymobile site. One of the pages has a search box with
I have a page that is straight from the jQueryMobile site. I'm including a
I have an issue with the footer of a mobile site built using jQuery
Have just started using Google Chrome , and noticed in parts of our site,
I have created a mobile version of my website using jquery mobile. My site
I have simple jQuery Mobile site created using asp.net mvc 2 and uses basic

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.