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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T14:14:57+00:00 2026-05-15T14:14:57+00:00

If you use append() to insert a new DOM node, is it possible to

  • 0

If you use append() to insert a new DOM node, is it possible to move the chain context to the new element?

This sample code illustrates the subject:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" 
                      "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head><title></title>
<script type="text/javascript" 
        src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js">
</script>
<script type="text/javascript"><!--
$(function(){
    $("<div></div>")
        .appendTo("body")
        .append("<span>Click me</span>")
        .click(function(){
            alert("Clicked on <" + this.tagName + ">, <SPAN> expected");
        });
});
//--></script>
</head>
<body>

</body>
</html>

This code attachs an onclick handler to the <div>, but I want it in the new <span>.

Edit: clarification

I suppose I was thinking about a direct function such as add() or andSelf() that doesn’t break the chain flow so much <:-)

Edit: solution summary

For the records: there doesn’t seem to be a native findAppended() function. These are some of the alternatives:

Alter creation flow:

$("<span>Click me</span>")
    .appendTo("body")
    .wrap("<div></div>")
    .click(function(){
        alert("Clicked on <" + this.tagName + ">, <SPAN> expected");
    });

Store the new element in a variable:

var span = $("<span>Click me</span>")
    .click(function(){
        alert("Clicked on <" + this.tagName + ">, <SPAN> expected");
    });
$("<div></div>")
    .appendTo("body")
    .append(span);

Start a new subchain:

$("<div></div>")
    .appendTo("body")
    .append(
        $("<span>Click me</span>")
        .click(function(){
            alert("Clicked on <" + this.tagName + ">, <SPAN> expected");
        })
    );

Find the new element after creating it:

$("<div></div>")
    .appendTo("body")
    .append("<span>Click me</span>")
    .find("span:last")
    .click(function(){
        alert("Clicked on <" + this.tagName + ">, <SPAN> expected");
    })
    .end(); // Not necessary here, used to illustrate how to continue the chain

Note: code not tested.

  • 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-15T14:14:58+00:00Added an answer on May 15, 2026 at 2:14 pm

    Might be better to break that up a bit, but you need to start with the element or find it. So you could either:

     $("<div></div>")
            .appendTo("body")
            .append("<span>Click me</span>")
            .find('span')
            .click(function(){
                alert("Clicked on <" + this.tagName + ">, <SPAN> expected");
            });
    

    or

    $('<span>Click me</span>')
         .appendTo("body")
         .wrap('<div />')
         .click(function(){
                    alert("Clicked on <" + this.tagName + ">, <SPAN> expected");
                });
    

    find and wrap

    Edit: regarding OP edit

    Keep in mind .add() (http://api.jquery.com/add/) already exists and does what the name implies. Also, something like

    var $span = $('<span>click me</span>').click( 
                     function() {
                         alert("Clicked on <" + this.tagName + ">, <SPAN> expected");
                     });
    $('<div></div>').append($span).appendTo('body');
    

    will ultimately make your code more manageable and readable

    jQuery already has classifying methods to filter and traverse the dom. In your case of the DOM, you could easily use .find(‘span’) to get to the element – or .children(‘span’) – use of good selectors and overall site architecture is important here.

    Also, the .end() function is pretty bad-ass:

    $('ul').append('<li>test</li><li>test2</li>') // add two li elements
        .children().eq(0).css('background-color', '#ff0000') // change the background of the first child
        .end().css('margin-left', '20px'); //revert to the original data-set (the ul) and change its margin
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 439k
  • Answers 439k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer seeing as how you have marked MVC in your tags,… May 15, 2026 at 4:57 pm
  • Editorial Team
    Editorial Team added an answer Assuming that you have an id column on users and… May 15, 2026 at 4:57 pm
  • Editorial Team
    Editorial Team added an answer It indicates that the file is read-only. May 15, 2026 at 4:57 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.