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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T15:32:55+00:00 2026-06-08T15:32:55+00:00

I have a div whose content gets dynamically generated from innerHTML and I cant

  • 0

I have a div whose content gets dynamically generated from innerHTML and I cant seem to target those elements using jQuery for validation, Eg This is the page:

<html xmlns="http://www.w3.org/1999/xhtml" dir="ltr" lang="en-US">
<head>
</head>
<body>
<span id="alert1">Alert1</span>
<input type="button" value="Click Me" id="button1" />
<div id="empty"></div>
</body>
</html>

and this is the js code:

<script type = "text/javascript"> 
    $(document).ready(function() {
         $('#button1').click(function() {
             var html = "uname <input type='text' id='uname'><br />password <input type='text' id='password'><br /><span id='alert'>Alert</span>";
             $('#empty').html(html);
         });
         $('#alert').click(function() {
             alert('Alert');
         });
         $('#alert1').click(function() {
             alert('Alert1');
         });
    });
</script>​

The alert for the span with id alert1 works but for id alert which is set using innerHTML or .html() does not work so how to fix this?

Thank you in advance
​

  • 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-08T15:32:56+00:00Added an answer on June 8, 2026 at 3:32 pm

    The problem here is that when you write like this..

        $("#element").click(function() {
            alert("Alert!");
        });
    

    .. it will only create events handlers for elements that exist when you execute the code. It won’t create event handlers for future elements even if they have the same jQuery selector (“#element” in this case).

    What you need to do is bind with .on(event, selector, function) on a parent element, as the events “bubble up” the hierarchy. That way you can handle future events from the specified selector. Personally I would do it like this:

    $(document).ready(function() {
         $('#button1').click(function() {
             var html = "<insert lots of html stuff here>";
             $('#empty').html(html);
        });
    
        $(document).on("click", "#alert", function() {
            alert("Alert!");
        });
    
        $(document).on("click", "#alert1", function() {
            alert("Alert1!");
        });
    });​
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

So I have a div that gets content dynamically, and when the content size
I have a Fixed positioned DIV whose content is appended using jquery. When its
I'm using Ajax to dynamically populate a DIV. I have each page content stored
Say I have some HTML elements: <div>First</div> <div>Second</div> <div>Third</div> Whose content I select via
So I have a div whose content is generated at runtime it initially has
I have multiple divs in a page whose content gets loaded via ajax. I
I have a div whose content may change in various ways: for instance its
I have a positioned div whose content can be too long so scrollbars appear
I have some one page whose div elements are aligned by JavaScript. The JavaScript
I am using jQuery's $.get() to load some content to a div whose id=results

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.