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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T08:36:22+00:00 2026-05-15T08:36:22+00:00

I have an HTML Table consisting of several rows. I am trying to insert

  • 0

I have an HTML Table consisting of several rows. I am trying to insert rows at a specific position in the table. For example if I mark an already existing row and click insert a new row should be inserted below. At the moment adding rows at the bottom of the table works. What I need is to insert the already build row at a certain position.

Here is my code:

function addLine(lineNumberGlobal,columnnumber,insertion)
{
  var newrow = document.createElement("tr");
  newrow.setAttribute('id', globalObj.lineNumberGlobal);
  newrow.setAttribute('onmousedown', "setCurIdG(getAttribute(\"id\"))");

  for (j = 1; j <= globalObj.monthdays + 1; j++)
  {
    //build row cells with content
  } 

  if (insertion == true)
  {
    var newrowinsert = globalObj.bodyGlobal.insertRow(globalObj.currenIdGlobal);
    //this actually inserts a new empty row but I want to append my existing row "newrow"
  }
  else if (insertion == false)
  {
    globalObj.bodyGlobal.appendChild(newrow);
  } 

}

Any help would be appreciated …

  • 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-15T08:36:22+00:00Added an answer on May 15, 2026 at 8:36 am

    You can use the insertBefore DOM method. If you want to insert after the row that you have marked, you would need to use that row’s nextSibling to find the row after it, and then insert before that.

    If I assume that globalObj.currenIdGlobal is the ID of the row you want to insert after, that would look like this:

    var refElement = document.getElementById(globalObj.currenIdGlobal);
    if (refElement) { // Being defensive here, you probably know it _does_ exist
        globalObj.bodyGlobal.insertBefore(newrow, refElement.nextSibling);
    }
    

    That assumes that your HTML is structured with no whitespace or similar between rows (since nextSibling will return the next node after the row, which can be a text node rather than an element). If you need to be more defensive:

    function findNextSiblingElement(elm, tagName) {
        do {
            elm = elm.nextSibling;
        } while (elm && (elm.nodeType != 1 || elm.tagName != tagName));
        return elm;
    }
    

    and then change the above to:

    var refElement = document.getElementById(globalObj.currenIdGlobal);
    if (refElement) { // Being defensive here, you probably know it _does_ exist
        globalObj.bodyGlobal.insertBefore(newrow, findNextSiblingElement(refElement, 'TR'));
    }
    

    Note that if you pass null as the second argument to insertBefore, it appends to the end.

    FWIW, operations like these can be made a bit easier if you use a library like Prototype, jQuery, Closure, or any of several others.

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

Sidebar

Related Questions

I have a HTML table with several columns and several rows. The last row
I have an HTML table with several rows. If you hover on the row
I have an html table with many rows. I'm currently grouping several rows inside
I have html table with 3 rows each row contains two <td> one with
I have an HTML table with several columns and I need to implement a
I have an HTML table with a large number of rows, and I need
I have an HTML table where the first cell of each row contains text
I have a html table which consists of columns for days, and rows for
I have the following problem: I need to insert N rows after row X.
I have a html table and I want to freeze the header row th

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.