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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T21:04:28+00:00 2026-06-06T21:04:28+00:00

I am using .on() to attach click events to multiple elements that appear on

  • 0

I am using .on() to attach click events to multiple elements that appear on the page dynamically. The problem I have is that when I add .on to a container on the page and want to attach click events to multiple elements in the container, the latter overwrites the previous.

<div id="container">
   <!-- elements here appear dynamically -->
   <div id="id1"></div>
   <div id="id1"></div>
</div>

 <script>
   $('#container').on("click", "#id1", function(){});
   $('#container').on("click", "#id2", function(){});
 </script>

In the above example only the click event for id2 works.

Is there a way around this?

Thank you,
Ev.

  • 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-06T21:04:29+00:00Added an answer on June 6, 2026 at 9:04 pm

    Demo http://jsfiddle.net/aRBY4/2/ little improvement here http://jsfiddle.net/aRBY4/5/

    Yes your id is wrong. :)

    YOu are using same id i.e. id1 for both elements.

    Hope this helps,

    code

    bit improved code

    $('#container').on("click", "#id1, #id2", function() {
        alert($(this).prop('id')) // you can use --> attr('id')
    });​
    

    or

     $('#container').on("click", "#id1, #id2", function() {
            alert($(this).attr('id')) 
        });​
    

    or

     $('#container').on("click", "#id1", function(){alert('d1')});
       $('#container').on("click", "#id2", function(){alert('d2')});​
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to attach a single event handler to multiple DOM elements using jQuery.
I have a user control. It contrains LinkButton. I want to attach an click
Basically I have an IFRAME that displays a given page. I am using jQuery
I want to attach an event to an element in JavaScript, problem is that
I am using jQuery to attach a function to my click event for an
I create a <select> menu using Javascript then attach it to the page. But
I've read that using Object.prototype to attach functions to custom JavaScript objects is more
I am using the following loop to send/attach multiple attachments to my email message.
I'm trying to find a simple way to attach click events to an element,
I have a search page that loads results. Each row has a Delete button

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.