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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T12:28:58+00:00 2026-05-22T12:28:58+00:00

In this very simplified and minimized example click event handler has been defined for

  • 0

In this very simplified and minimized example click event handler has been defined for every a tag (simple alert is displayed after link is clicked). Once you click the button, second link is added to the page.

But if you click this newly added link, event handler wan’t be called. Any idea why? What should be added/changed so that newly added elements are aware of the scripts defined on the main page?

<html>
<head>
    <script type="text/javascript" src="http://code.jquery.com/jquery-1.6.1.min.js"></script>
    <script type="text/javascript">
        $("document").ready(function() {
            $("a").click(function() {
                alert("link is clicked");
                return false;
            });
            $("#btn").click(function() {
                $("#second").html("<a href='#'>Second link</a>");
            });
        });
</script>
</head>
<body>
    <div id="first">
        <input id="btn" type="button" value="Get new content"/>
        <a href="#">Original link</a>
</div>
<div id="second"/>
</body>
</html>

In this particular case, what should be done so that second link is aware of event handler defined in script section so that alert is being displayed after link is clicked?

What’s your advice on resolving these kind of situations? How do you make newly added elements on the page be aware of previously defined Javascript code?

  • 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-05-22T12:28:59+00:00Added an answer on May 22, 2026 at 12:28 pm

    Because you’re binding to all the existing links on the page. Try:

    $('a').live('click', function(){
        alert('link is clicked');
        return false;
    });
    

    jQuery’s live function actually attaches an event handler to document.body and then checks to see if the event matches when it eventually bubbles up to body. So it will work for all current and future objects on the page.

    Note that if these links are in a particular container, this code is more efficient:

    $('#container').delegate('a', 'click', function(){
        ...
    });
    

    delegate only checks events that bubble up to #container, so jQuery doesn’t check against every click anywhere on the page.

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

Sidebar

Related Questions

Have a look at this very simple example WPF program: <Window x:Class=WpfApplication1.Window1 xmlns=http://schemas.microsoft.com/winfx/2006/xaml/presentation xmlns:x=http://schemas.microsoft.com/winfx/2006/xaml
This very simple code gives me tons of errors: #include <iostream> #include <string> int
I've written this very simple function to replace a file extension using LINQ in
i am trying to compile this very simple piece of code class myList {
my client has asked me to make exactly what's happening in this jsFiddle example
This is a very simplified version of a model I'm working on: class ClothingTop(models.Model):
Let's make this very easy. What I want: @array = qw/one two one/; my
I'm sorry for this very newbish question, I'm not much given into web development.
I find this very strange, must be something I'm doing wrong, but still... I'm
I was told this a few times in this very site, but I wanted

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.