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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T15:29:16+00:00 2026-06-09T15:29:16+00:00

I have the below code <div> <p>some text</p> <p>some text</p> <p>some text</p> <p>some text</p>

  • 0

I have the below code

<div>
    <p>some text</p>
    <p>some text</p>
    <p>some text</p>
    <p>some text</p>
    <p>some text</p>
    <p>some text</p>
    <p>some text</p>
</div>

with this jquery

jQuery('p:nth-child(5)').after('</div><div><img src="http://dummyimage.com/100x100/fff/000"></div><div>');

I want my output to be this.

<div>
    <p>some text</p>
    <p>some text</p>
    <p>some text</p>
    <p>some text</p>
    <p>some text</p>
    </div><div><img src="http://dummyimage.com/100x100/fff/000"></div><div>
    <p>some text</p>
    <p>some text</p>
</div>

instead I am getting this.

<div>
    <p>some text</p>
    <p>some text</p>
    <p>some text</p>
    <p>some text</p>
    <p>some text</p><div><img src="http://dummyimage.com/100x100/fff/000"></div><div></div>
    <p>some text</p>
    <p>some text</p>
</div>

see this fiddle for example.

Where am I going wrong here? Or should I be using something other than .after() to do this?

  • 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-09T15:29:17+00:00Added an answer on June 9, 2026 at 3:29 pm

    You can’t use unbalanced HTML to change the DOM tree like that. jQuery requires a balanced block of HTML to be supplied, since each time you pass in HTML like this it’s creating a detached DOM tree, not merely inserting the HTML snippet into the source code.

    You’ll need to create a new <div> to hold the image, and another to hold the remaining <p> elements, and then use .append() to move the excess elements from the first div into the latter, e.g.:

    var $d = $('div');
    var $p = $('p:gt(4)');
    if ($p.length) {
        $('<div>').insertAfter($d).append($p);
        $('<div><img src="..."></div>').insertAfter($d);
    }
    

    See http://jsfiddle.net/alnitak/Z8LR3/

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

Sidebar

Related Questions

I have some code below: <!doctype html> <html lang=en> <head> <meta http-equiv=Content-Type content=text/html; charset=utf-8
I have below code: <a href=# id=@item.Id name=vote ><img src=/Content/images/021.png style=float:left alt= /></a> which
I have the code as per below. This retrieves some data to use for
I have below html code <div id=divTest> Hello <input type=text id=txtID value= /> <input
I have the below HTML and JQuery code and i am needing some help
I have below html code in one of the opensource project. <form action=/wiki/bin/view/Main/Search> <div
I have the following code below however am confused as to why the div
I have the following code below for list into master page <div id=header> <ul>
Please have a look on the code below .. <div id=click_me>Save</div> <div id=blocks_sortable> <div
I have below code behind in c# if (Session[cmpDictionaryTitle]!= null) { downloadLinks.Text += @<li><a

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.