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

  • Home
  • SEARCH
  • 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 1066327
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T19:59:52+00:00 2026-05-16T19:59:52+00:00

I have this code (running jQuery 1.4.2) var elementToAdd = $(‘<h3>’).html(‘header’); var p =

  • 0

I have this code (running jQuery 1.4.2)

var elementToAdd = $('<h3>').html('header');
var p = $('<p>').html('hello world');
elementToAdd.after(p);
$('div#content').append(elementToAdd);

However, the output is

<div id="content">
  <h3>header</h3>
</div>

The paragraph “Hello world” is not added.

What am I doing wrong?


I have been trying out some variations:

This does not work either:

var elementToAdd = $('<div>Header</div>');
var p = $('<p>hello world</p>');
elementToAdd.after(p);

or this:

var elementToAdd = $('<h3>header</h3>').after('<p>hello world</p>');

But this works (on Firefox, at least):

var elementToAdd = $('<div>').after('<h3>header</h3>').after('<p>hello world</p>');

Why?

  • 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-16T19:59:53+00:00Added an answer on May 16, 2026 at 7:59 pm

    EDIT: To modify the original set, you can .push() a DOM element (not a jQuery object) into the set.

    var elementToAdd = $('<h3>').html('header');
    var p = $('<p>').html('hello world');
    
       // Push the DOM element in
    elementToAdd.push( p[0] );   // [0] gets the DOM element at index 0
    $('div#container').append( elementToAdd );​
    

    Instead of .after(), use jQuery’s .add() method.

    var elementToAdd = $('<h3>').html('header');
    var p = $('<p>').html('hello world');
    
     // Add the new object ----------------v
    $('div#container').append( elementToAdd.add(p) );​
    

    This will append it as a sibling like you want.

    It returns a new jQuery object with both elements as siblings. Because it doesn’t modify the original object, you need to either call it in the .append() or store the result in a variable to append.


    EDIT: (As noted in another answer, you can now use after() like add(), in that it returns a new set and doesn’t modify the original.)

    To explain why, this is because a jQuery object is an Array of DOM elements. A DOM element can be a single element with nested descendants, but not two siblings. So when you do .after(), you’re trying to add a sibling to each single element in the array.

    To deal with siblings, jQuery has them stored as additional items in its Array.

    So when you create a jQuery object by passing 2 or more sibling elements, it splits them apart, and makes them separate items in the Array.

    var $obj = $("<div>div element</div> <span>span element</span>");
    

    This will give you a jQuery object with an Array of 2 items.

    $obj[0] is the <div> element
    $obj[1] is the <span> element
    

    So if you were to create them separately, you would need to use .add() to add the new item to the Array.

    var $obj = $("<div>div element</div>");
    
    $obj[0] is the <div>
    
    var $span = $("<span>span element</span>");
    $obj = $obj.add( $span );
    
    $obj[0] is the <div>
    $obj[1] is the <span>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this code running in my onDestroy function: @Override protected void onDestroy() {
I have this code running great in ff, opera and chrome: <script type=text/javascript> $(document).ready(function(){
Code is not running on .click when I have this: $(.cancel).click(function() { alert(got here);
I have been using this code, but have been running into some memory issues:
I'm a beginner with jdbc ... I have a problem running this code :
I have this code <div id=main style=background:#aaaaaa;float:left;height:160px;margin:5px;position:relative;display:block;width:630px;> <div id=1 class=item style=background:#ffaacc;float:left;width:200px;height:150px;margin:5px;position:absolute;left:0px;top:0px;> </div> <div id=2
I have the following HTML code: <div id=summary_form> <textarea class=summary>Please fill in</textarea><br/> <textarea class=summary>Please
I have written this code to benchmark jQuery vs DOM performance. The performance is
I am running into this Jquery div show() hide() problem when you click on
I'm not too sure why my code is causing this. I have this jquery

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.