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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T12:52:59+00:00 2026-05-31T12:52:59+00:00

I’m having an issue using .on() to appended elements. I’m trying to select and

  • 0

I’m having an issue using .on() to appended elements.

I’m trying to select and dynamically create divs that have the ability to be selected and have divs added to them. The problem I’m having is that when I select a dyanmic element that is inside an element that uses .on()it selects the parent too. I’m fairly new to jQuery so there may be a totally better solution.

<script type="text/javascript">
$(function () {
    var wrapper = $('#wrapper');
    var add_row_button = $('#add_row');        

    var row_count = 0;
    var selected_item = wrapper;

    add_row_button.click(function () {

        row_count++;

        var new_element = '<div id="' + row_count + '">' + row_count + '</div>';

        selected_item.append(new_element);

        $('#' + row_count).on("click", function () {
            console.log(selected_item);
            selected_item.removeClass("selected");
            selected_item = $(this);
            selected_item.addClass("selected");
            console.log(selected_item);
        });
    });        
});    

<button id="add_row">Add row</button>
<div id="wrapper"></div>
  • 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-31T12:53:00+00:00Added an answer on May 31, 2026 at 12:53 pm

    You haven’t shown us your actual HTML, but here’s an example that could work for you.

    The idea of delegated event handling is that you attach an event handler to a parent object that is static (doesn’t come and go). Events on child objects then bubble up to the parent and the jQuery event handler on the parent checks the source of the event to see if it matches the desired selector.

    So, if your container for these dynamically created events was #container and each dynamically create child had a class name of dynChild, you could hook up a click handler for all children matching that criteria with this:

    <div id="container">
       <div class="dynChild"></div>
       <div class="dynChild"></div>
       <div class="dynChild"></div>
       <div class="dynChild"></div>
    </div>
    
    $('#container').on('click', '.dynChild', function() {
        // handle click on a child here
        // The this pointer will point to the element that was clicked on
    });
    

    You can then add/remove matching child objects without changing the event handling and their click events will automatically be handled by this delegated event handler on the parent object. It’s one of the more powerful capabilities for managing dynamically created/modified pages simply.

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

Sidebar

Related Questions

I'm trying to create an if statement in PHP that prevents a single post
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
Basically, what I'm trying to create is a page of div tags, each has
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build
I have thousands of HTML files to process using Groovy/Java and I need to
I am trying to loop through a bunch of documents I have to put

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.