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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T05:47:30+00:00 2026-06-17T05:47:30+00:00

Via a click event, I injected an element with a custom attribute <div class=added

  • 0

Via a click event, I injected an element with a custom attribute
<div class="added" myValue="hello"></div> to the DOM.

I wrote a generic function:

$('.added').each(function(){
    console.log( $(this).attr('myValue') );
});

Doesn’t work. I thought $.each() can handle dynamic elements?

UPDATE to the question:

I realize using .each() is the wrong approach. But what to do if I don’t want to attach an event handler to the element. I just want to automatically do stuff to any elements of a certain class name, regardless of when they’ve been added to the DOM. Seems like a common use case.

  • 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-17T05:47:31+00:00Added an answer on June 17, 2026 at 5:47 am

    When your click event happens, do your “something” there.

    $("#someelemt").click(function(){
        var el = $("<div class='added' myValue='hello'></div>").appendTo("#someotherelement");
        console.log(el.attr("myValue")); // <--- something
    });
    

    ID’s must be unique, so i changed added to a class.

    If you want to separate the two, have your click event trigger an event, then use event delegation to listen for it.

    $("#someelemt").click(function(){
        var el = $("<div class='added' myValue='hello'></div>").appendTo("#someotherelement");
        el.trigger("added");
    });
    $(document).on("added","div",function(){
        console.log(this.getAttribute("myValue")); // <--- something
    });
    

    $.each does not work with future elements, only the elements that exist when the code is executed.

    Update for question Update
    That simply isn’t possible without writing inefficient code. It would require either using DOMMutationEvents which can be inefficient, are depreciated, and are not supported in all browsers, or you can use a setInterval which is also inefficient because it would be constantly running searching the dom for new elements.

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

Sidebar

Related Questions

I have this code $(.delete2).click(function() { $('#load2').fadeIn(); } I have dynamically added item via
I am updating a component via AJAX in JSF: <h:form> <h:outputLink>Click me <f:ajax event=click
I am trying to add a click event on the element that is return
$(document).on('click', '#slider_icons_wrapper.play', function(event) { var slider_icons_wrapper = $('#slider_icons_wrapper'); slider_icons_wrapper.attr('class', 'pause'); autoSlider(cur_img_div_pos); showButton('play_icon'); console.log('play'); });
I am currently adding an input via a .click event and then wanting to
So I am injecting html via an AJAX request: Ajax Response <div id=my-element>HTML from
I am trying to create a click event via jQuery Here is the HTML
When you attach a client-side click event via jQuery to an ASP.NET button do
Is that possible to fire Flash-button click-event via javascript ? it`s my code ,
I got a table and i assign the click event via .live() to each

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.