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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T13:25:27+00:00 2026-06-12T13:25:27+00:00

the jQuery ‘on’ function is supposed to catch events for elements created in the

  • 0

the jQuery ‘on’ function is supposed to catch events for elements created in the future but in my code it doesn’t seem to work unless the item has already been created. Here is the code that fails:

<!doctype html>
<html>
<head>
<script type="text/javascript" src="js/jquery-1.7.2.min.js"></script>
<script type="text/javascript">
$(function () {
    $('#div1').on('click', function(e) { 
        $('#div2').html('<div id="div3">Now Click Me!</div>'); 
    });
    $('#div3').on('click', function(e) { alert('OLA!'); });
});
</script>
</head>
<body>
<div id='div1' style='border:black 1px solid; background:yellow'>CLICK ME</div>
<div id='div2' />
</body>
</html>

Clicking ‘div2’ creates ‘div3’, but then clicking on ‘div3’ does nothing. On the other hand, if I change the javascript code to look like this:

$(function () {
    $('#div1').on('click', function(e) { 
          $('#div2').html('<div id="div3">Now Click Me!</div>'); 
          $('#div3').on('click', function(e) { alert('OLA!'); });
    });
});

it works, but it works because the event handler for ‘div3’ is no being declared AFTER the div3 element has been added. Perhaps I am misunderstanding how ‘on’ is supposed to work?

  • 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-12T13:25:28+00:00Added an answer on June 12, 2026 at 1:25 pm

    Try this…

    $('body').on('click', '#div3', function(e) { alert('OLA!'); });
    

    The first selector needs to be a common ancestor element that the events will bubble to.

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

Sidebar

Related Questions

jQuery's 'hover' trigger doesn't seem to work. My code is simple: <div id='el'>Content</div> //
jQuery's draggable functionality doesn't seem to work on tables (in FF3 or Safari). It's
jQuery code: function ajaxsubmit(){ $.ajax({ url: /update, type: POST, dataType: html }).success(function(data) { $('#result').html(data);
Jquery: <script type=text/javascript src=http://code.jquery.com/jquery-1.7.2.min.js></script> <script type=text/javascript src=js/timeago.js ></script> $(document).ready(function(){ jQuery(abbr.timeago).timeago(); }); html: <abbr class=timeago
jQuery code add new virtual object .add to object .main $(document).ready(function(){ $('.main').on('click', '.add', function
jQuery code: $(document).bind('click', function(e) { console.log(e.target); alert(e.target); }); The e.target contains the name of
jQuery's mouseenter doesn't seem to fire when the mouse entered an element because of
Jquery UI autocomplete wont work,what could be wrong?Here is the code.Thank you <head> <meta
jQuery.unique lets you get unique elements of an array, but the docs say the
jQuery code is $(document).ready(function() { if (navigator.appVersion.indexOf(Win) != -1) { // Computers runs windows

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.