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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T18:58:29+00:00 2026-05-26T18:58:29+00:00

Suppose I have the following HTML and js <div> <div class=testdiv id=test1>test</div> <div class=testdiv

  • 0

Suppose I have the following HTML and js

<div>
  <div class="testdiv" id="test1">test</div>
  <div class="testdiv" id="test2">test</div>
</div>

function DynamicEvents() {

  $('.testdiv').click (function () {
     DoSomething();
  });
}

function RedrawHTML() {

  $('#test1').html('new HTML');
  DynamicEvents();
}

Everytime I’m calling RedrawHTML, I’m also calling DynamicEvents to rebind test1. Suppose I’m calling RedrawHTML several times to update the div. My question is this: when I click on test2, does the function DoSomething() get executed once or get executed as many times as I’ve called RedrawHTML()?

  • 1 1 Answer
  • 3 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-26T18:58:30+00:00Added an answer on May 26, 2026 at 6:58 pm

    In order to properly use .on you need to bind the event(s) to a parent element that is currently present in the DOM. So if i were you i would give the parent div an id and bind the event to that. Here’s an example

    For your html

    <div id="container">
      <div class="testdiv" id="test1">test</div>
      <div class="testdiv" id="test2">test</div>
    </div>
    

    In the JS

    $('#container').on('click', '.testdiv', DoSomething); 
    

    If there’s no parent element guaranteed to be in the DOM the time the event is bound, then you could in the worst case bind to the body or document but i do not recommend this approach.

    $("body").on('click', '.testdiv', DoSomething); 
    
    $(document).on('click', '.testdiv', DoSomething); 
    

    If you’re not using jQuery 1.7, the equivalent delegate code would be

     $('#container').delegate('click', '.testdiv', DoSomething); 
    

    You only need to do this once, all click events triggered by .testdiv will bubble up to the #container and DoSomething() will be executed.

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

Sidebar

Related Questions

Suppose I have the following html: This a test of <code>some code</code>. <div class='highlight'>
Suppose I have the following HTML: <div id=test> <span style=display:inline;>foo</span> <span style=display:none;>bar</span> <span style=display:inline;>baz</span>
suppose I have following test/ Main.py test/one/ One.py test1.txt test/two/ Two.py test2.txt What I
Suppose I have the following HTML: <div class=container> <span class=remove>remove</span> </div> and jquery: $(.container).delegate(.remove,
Suppose I have the following HTML: <div id=Wrapper> <div class=MyClass></div> <div class=MyClass></div> <div class=MyClass></div>
Suppose you have this following code in HTML - <div id=pattern> <span class=a>Content</span> </div>
Suppose I have the following html: var testString = ' <span id='id'> <div> <table>
As following, suppose I have a click button in A.html to open a ui
Fellow JQuery hackers, hello :-) Suppose you have the following code: $('.links').click(function(){ //Do something
I have the following html structure: <div class=divClass> <a class=aClass href=#> <img src=xxxx.jpg <span

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.