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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T06:03:22+00:00 2026-05-27T06:03:22+00:00

Please vote for me which one in the below list is better? I have

  • 0

Please vote for me which one in the below list is better?

I have HTML:

<div id="container">
    <button class="btn">Click Here 1</button>
    <button class="btn">Click Here 2</button>
    <button class="btn">Click Here 3</button>
    <button class="btn">Click Here 4</button>
    <button class="btn">Click Here 5</button>
    <button class="btn">Click Here 6</button>
    <!-- A lot of buttons -->
    <button class="btn">Click Here n - 2</button>
    <button class="btn">Click Here n - 1</button>
    <button class="btn">Click Here n</button> 
</div>

And Javascript with jQuery is:

Case 1.1:

$(".btn").click(function(e){
    //@todo something here
});

Case 1.2:

var doSomething = function(e)
{
    //@todo something here
}
$(".btn").click(doSomething);

Case 2:

$("#container").click(function(e){
    if( $(e.target).is(".btn") )
    {
        //@todo something here
    }
});

I am confused a litle bit what are different between them?

  • 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-27T06:03:23+00:00Added an answer on May 27, 2026 at 6:03 am

    You should use the new jQuery on() function

    $(document).on("click", ".btn", doSomething);
    

    so that

    • dynamically added content will be covered
    • jQuery won’t have to waste time searching out, and wrapping all affected buttons with the handler; all clicks will propogate up to the context, and jQuery will apply them if they match the selector—.btn in this case

    If you know that all your buttons will be in #container, then you would use that as the context, instead of document.

    $("#container").on("click", ".btn", doSomething);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

**EDIT: There are several options below that would work. Please vote/comment according to your
Please one library per answer so that people can vote for the individually.
Please consider this example class: [Serializable] public class SomeClass { private DateTime _SomeDateTime; public
I've got a variable which is formatted with random HTML code. I call it
The app allows users to vote on embedded videos. When users click up and
I'm implementing a vote system and I want to use jQuery. I have the
[Please vote to close this - see my last comment.] Hi, Something like this:
I have a program, it starts in a winmain which calls run_game in its
I have this code which I plastered with return false; so that it wouldn't
In my app I have the classes User, Video, and Vote. Users and Videos

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.