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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T15:27:05+00:00 2026-05-11T15:27:05+00:00

I have the following problem: I need to insert N rows after row X.

  • 0

I have the following problem: I need to insert N rows after row X. The set of rows I need to insert is passed to my function as chunk of HTML consisting of TR elements. I also have access to the TR after which I need to insert.

This is slightly different then what I have done before where I was replacing TBODY with another TBODY.

The problem I am having is that appendChild requires a single element, but I have to insert a set.

Edit:

Here is the solution :

function appendRows(node, html){      var temp = document.createElement('div');     var tbody = node.parentNode;     var nextSib = node.nextSibling;      temp.innerHTML = '<table><tbody>'+html;     var rows = temp.firstChild.firstChild.childNodes;      while(rows.length){         tbody.insertBefore(rows[i], nextSib);     } }  

see this in action:

http://programmingdrunk.com/test.htm

  • 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. 2026-05-11T15:27:06+00:00Added an answer on May 11, 2026 at 3:27 pm
    if(node.nextSibling && node.nextSibling.nodeName.toLowerCase() === 'tr') 

    What’s this for? I don’t think you need it. If node.nextSibling is null, it doesn’t matter. You can pass that to insertBefore and it will act the same as appendChild.

    And there’s no other element allowed inside a tbody than ‘tr’ anyway.

    for(var i = 0; i < rows.length; i++){     tbody.insertBefore(rows[i], node.nextSibling); 

    This won’t work for multiple rows. Once you’ve done one insertBefore, node.nextSibling will now point to the row you just inserted; you’ll end up inserting all your rows in reverse order. You’ll need to remember the original nextSibling.

    ETA: plus, if ‘rows’ is a live DOM NodeList, every time you insert one of the rows into the new body, it removes it from the old body! Thus, you are destroying the list as you iterate over it. This is a common cause of ‘every other one’ errors: you process item 0 of the list, and in doing so remove it from the list, moving item 1 down into where item 0 was. Next you access the new item 1, which is the original item 2, and the original item 1 never gets seen.

    Either make a copy of the list in a normal non-live ‘Array’, or, if you know it’s going to be a live list, you can actually use a simpler form of loop:

    var parent= node.parentNode; var next= node.nextSibling; while (rows.length!=0)     parent.insertBefore(rows[0], next); 

    i have to insert a set.

    Usually when you think about inserting a set of elements at once, you want to be using a DocumentFragment. However, unfortunately, you can’t set ‘innerHTML’ on a DocumentFragment, so you’d have to set the rows on a table like above, then move them one by one into a DocumentFragment, then insertBefore the documentFragment. Whilst this could theoretically be faster than appending into the final target table (due to less childNodes list-bashing), in practice by my testing it isn’t actually reliably significantly faster.

    Another approach is insertAdjacentHTML, an IE only extension method. You can’t call insertAdjacentHTML on the child of a tbody, unfortunately, in the same way as you can’t set tbody.innerHTML due to the IE bug. You can set it inside a DocumentFragment:

    var frag= document.createDocumentFragment(); var div= document.createElement(div); frag.appendChild(div); div.insertAdjacentHTML('afterEnd', html); frag.removeChild(div); node.parentNode.insertBefore(frag, node.nextSibling); 

    Unfortunately, somehow insertAdjacentHTML works very slowly in this context for some mysterious reason. The above method is about half the speed of the one-by-one insertBefore for me. 🙁

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

Sidebar

Ask A Question

Stats

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

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

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

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

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Do you really need a (traditional) plugin based solution to… May 12, 2026 at 4:00 pm
  • Editorial Team
    Editorial Team added an answer 'seq' is a lazily-evaluated construct; it could be infinite, which… May 12, 2026 at 4:00 pm
  • Editorial Team
    Editorial Team added an answer You basically have three options: Store the license keys in… May 12, 2026 at 4:00 pm

Related Questions

I'm starting to use objective C, and objective C mode works perfectly fine. However,
I'm working on a multi-user internet database-driven website with SQL Server 2008 / LinqToSQL
Let's say that in the controller I get an array of objects from the
I have a table. The table needs to store a number values about a

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.