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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T10:22:16+00:00 2026-06-15T10:22:16+00:00

I have some divs being created dynamically and I’m using bind/live (tried both, same

  • 0

I have some divs being created dynamically and I’m using bind/live (tried both, same results) to add a click function. However, it is returning all the divs with the same class.

Example;

I have 3 dynamically generated divs

1
2
3

If I click on 2, the alert method goes to show 1 then 2 then 3. I only need the one clicked. Thanks!

$("#borang_main_form").append('<div class="borang-row" id="'+id+'"><label id="label_'+id+'"></label><input id="textbox_'+id+'" type="text" /></div>').bind("click", function(){alert(id);});

I’ve tried this and it works.

$("#borang_main_form").append('<div class="borang-row" id="'+id+'"><label id="label_'+id+'"></label><input id="textbox_'+id+'" type="text" /></div>').delegate("#" + id, "click", function(){alert(id);});

However it looks a little dirty. Is there a better way to approach something like this?

  • 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-15T10:22:17+00:00Added an answer on June 15, 2026 at 10:22 am

    When you iterate with your loop, you use method chaining, which basically end up adding n number of event bindings to the same parent item (“#borang_main_form”)

    In your loop first append the items, then outside the loop add the event listeners to the individual children elements.

    for(var i=0; i<3; i++)
    {
      var id = i;
      $("#borang_main_form").append('<div class="borang-row" id="'+id+'"><label id="label_'+id+'"></label><input id="textbox_'+id+'" type="text" /></div>')
    }
    $('.borang-row').on("click", function(){alert($(this).attr('id'));});
    

    In the event handler function $(this) will be mapped as the item that was clicked (the trigger of the event) and through $(this).attr('id') you will get the id attribute of that element.

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

Sidebar

Related Questions

I'm having a problem with some jquery animations. I have dynamically created divs with
I have some DIVs that I am using JQuery to hide and show using
I have some divs in the page that show different things of the same
I have some divs: <div class=A>Target</div> <div class=A B>NotMyTarget</div> <div class=A C>NotMyTarget</div> <div class=A
I have some divs that have dynamic heights controlled by a 'click' function as
I have some divs that have dynamic heights controlled by a 'click' function as
Scenerio: I have some divs populated through foreach loop like below <div id=div1><input type=hidden
I have some forms, which are in some DIVs. DIVs are hidden, and are
I have this code Math.min(someVariable, 500)) I also have some divs which I have
I have some icons inside divs and needed to lay them out with a

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.