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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T05:47:55+00:00 2026-06-06T05:47:55+00:00

I am curious why jQuery’s .after() does not chain, or provide you with, the

  • 0

I am curious why jQuery’s .after() does not chain, or “provide you with,” the new element that you create with it. It seems to me like it should, but I am no expert and was hoping someone could shed some light on why it’s a bad idea. Here’s an example to see what I would like from .after():

Here is some markup:

<div id="content1">
    <p id="hello1">Hello</p>
</div>
<div id="content2">
    <p id="hello2">Hello</p>
</div>

Here’s what I want, but this replaces “Hello” with “World!”:

$('#hello1').after('<p />').text('World!');

This does the job, but it sure is ugly:

$('#hello2').after('<p />');
var $newParagraph = $('#content2 p').last();
$newParagraph.text('World');

This is nicer, but I’m not entirely sold: (maybe I should be?)

$('#hello1').after( $('<p />').text('World') );

Note this is obviously a simplified example, that is, “<p>World!</p>” is not what I want to add, my real world problem is more complex than that.

Thanks for any thoughts.

  • 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-06T05:47:57+00:00Added an answer on June 6, 2026 at 5:47 am

    Presumably .after() works as it does to allow you to chain more methods that would apply to the originally selected element(s):

    $('#hello1').after( $('<p />').text('World') )
                .before( $('<p />').text('Before text') )
                .remove();
    

    This keeps the behaviour consistent with other methods, so you don’t have to try to remember which ones return the original object and which return some other object.

    Note that in your case $('#hello1') selects exactly one element since you are selecting by id, but you can apply .after() to a jQuery object containing many elements to add (cloned) content after multiple elements:

    $('div.someclass').after( $('<p/>').text('whatever') );
    

    In which case in my opinion it definitely makes sense to keep the chaining context as the original jQuery object.

    The .insertAfter() method might be closer to what you want:

    $('<p/>').insertAfter('#hello1').text('World');
    

    (It also returns the jQuery object it was applied to, but it does the insert in “reverse”.)

    You could write your own plugin method to return the element being added, but that would be confusing since it would work opposite to all the standard methods.

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

Sidebar

Related Questions

I'm curious if its possible to create a custom event in jQuery that isn't
I was curious as to whether or not there exists a jQuery-style interface/library for
I'm curious how to specify options to a jQuery plugin in a way that
I'm curious how .fadeTo() fades an element? Does it use an inline style of
I am currently learning jQuery, and I am curious about something. For functions that
Curious problem. I have a set of images with some attributes that I'd like
I curious to know how the jQuery.load functionality affects page load speeds when reporting
I'm curious what the minimum required jQuery version is to use with the jQuery
I am a noivce at JQuery. I'm curious about if there is any plug-in
I've got a jQuery function that attempts to change the id , name and

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.