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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T00:38:17+00:00 2026-06-13T00:38:17+00:00

Possible Duplicate: jQuery click not working for dynamically created items $(document).ready(function(){ $(‘#btn’).click(function(){ var createInput=$(<input

  • 0

Possible Duplicate:
jQuery click not working for dynamically created items

        $(document).ready(function(){
            $('#btn').click(function(){

                var createInput=$("<input type='button'>");
                createInput.attr("id","close1");
                createInput.attr("value","close");
                $('#outer-div').append(createInput);
            });

            $("#close1").click(function(){
                alert('hi');
            });
        });


    </script>
</head>
<body>
    <div id='outer-div'>
        <input id='btn' type='button' value='click here'/>
    </div>

What’s wrong with the code. I am not getting the alert when i click the dynamically generated button named as close. Please help it out

  • 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-13T00:38:18+00:00Added an answer on June 13, 2026 at 12:38 am

    This would work :

        $(document).ready(function(){
            $('#btn').click(function(){
    
                var createInput=$("<input type='button'>");
                createInput.attr("id","close1");
                createInput.attr("value","close");
                $('#outer-div').append(createInput);
            });
    
            $(document).on('click', "#close1", function(){
                alert('hi');
            });
        });
    

    The problem with

    $("#close1").click...
    

    is that it adds an event handler to nothing, as the collection is empty when you execute this line. The on function fixes that.

    BTW, it’s recommended to put your script at the end of the body instead of in the head.

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

Sidebar

Related Questions

Possible Duplicate: jquery .click pass parameters to user function $('#topleft').click(function (x) { loadPopupBox(); $(#popupinner).load('getdetail.php?id='
Possible Duplicate: jquery on vs click methods I understand that $(parent).on('click', 'element', function(){ ...
Possible Duplicate: jQuery: prev(<selector>) not working? Consider this markup: <tr class=store> <td>...</td> <td>...</td> <td>...</td>
Possible Duplicate: In jQuery, why does programmatically triggering ‘click()’ on a checkbox not immediately
Possible Duplicate: Jquery event chaining Traditionally we may write: $(selector).click(function () { }); but
Possible Duplicate: How pass variable in jquery easy confirm Pass variable to click function
Possible Duplicate: jquery: select the last 4 items in the list jQuery has the
Possible Duplicate: Bind multiple events to jQuery 'live' method I have the following function:
Possible Duplicate: javascript not working in the new part when loading a new part
Possible Duplicate: Can I call jquery click() to follow an <a> link if I

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.