Sunday, October 28, 2007

The missing JavaScript insertAfter() function

I really have no idea why the XML/HTML DOM lacks an insertAfter() function. It seems like a pretty common operation. Thankfully, the solution is simple. So, for those who are learning JavaScript and/or the DOM (and for those of us who are too lazy or tired to think), here's what we need to do, in effect:

function insertAfter(new_node, existing_node) {
  // if the existing node has a following sibling, insert the current
  // node before it. otherwise appending it to the parent node
  // will correctly place it just after the existing node.

  if (existing_node.nextSibling) {
    // there is a next sibling. insert before it using the mutual
    // parent's insertBefore() method.
    existing_node.parentNode.insertBefore(new_node, existing_node.nextSibling);
  } else {
    // there is no next sibling. append to the end of the parent's
    // node list.
    existing_node.parentNode.appendChild(new_node);
  }
} // insertAfter()


The function checks the existing node for a next sibling. If it has one, inserting after the current nodei is the same as inserting before the next.  If it doesn't have a next sibling, it assumes that the existing node is the last node under the parent. So, it may append the new node right under the parent.

Simple.

That should do it. Let me know if you have any problems with it.


ADDENDUM: As one commenter pointed out, insertBefore() should actually perform the correct appendChild() operation if existing_node.nextSibling is null. The function above makes this very explicit, which I tend to like. But, we can also simplify it, almost to the point of absurdity:

function insertAfter(new_node, existing_node) {
  existing_node.parentNode.insertBefore(new_node, existingNode.nextSibling || null);
} // insertAfter()


25 comments:

  1. Hi, your code has helped me. :) Many thanks, willowdan

    ReplyDelete
  2. Worked perfectly, thanks!

    ReplyDelete
    Replies
    1. HTML5 Training in Chennai HTML5 Training in Chennai JQuery Training in Chennai JQuery Training in Chennai JavaScript Training in Chennai JavaScript Training in Chennai Full Stack Developer Training in Chennai Full Stack Developer Training in Chennai


      AngularJS Training in Chennai AngularJS Training in Chennai Node.js Training in CHennai Angular 2 Training in Chennai Angular 2 Training in Chennai Node.js Training in CHennai Node.js Training in chennai MEAN Developer Training in Chennai

      Delete
  3. Excellent snippet. Thank you!

    ReplyDelete
  4. Thanks Jon - this is very helpful. I am not infatuated with you btw, but I like your sweet Javascript skillz.

    ReplyDelete
  5. Hi

    The test on existing_node.nextSibling is useless since insertBefore will have the right behaviour if existing_node.nextSibling is null (cf https://developer.mozilla.org/en/DOM/element.insertBefore).

    ReplyDelete
  6. You are correct, sir. I'm not entirely sure why I put an explicit test on nextSibling in this function at the time of this post.

    In any case, you can certainly omit the test on nextSibling unless you want some kind of special functionality in the case that the element is the last in the parent node.

    ReplyDelete
  7. Thanks! Very useful.

    ReplyDelete
  8. Hello everybody! can someone give me an exemple of use to this code, I'm trying it but it doesn't work, thanks

    ReplyDelete
  9. Thanks! Very useful.

    ReplyDelete
  10. Additionally, for certain elements you can pass in the value “-1″ before appending.
    For example:
    var row = table.insertRow(-1);
    for (var c= 0; c < 4; c++)
    {
    var cell = row.insertCell(-1);
    cell.appendChild(document.createTextNode(data[i++]));
    }

    ReplyDelete
  11. Very Elegant. Thanks.

    ReplyDelete
  12. This is my first visit to your blog, your post made productive reading, thank you. dot net training in chennai

    ReplyDelete
  13. Informative article, just what I was looking for.seo services chennai

    ReplyDelete
  14. Good post and I like it very much. By the way, anybody try this app development company for iOS and Android? I find it is so professional to help me boost app ranking and increase app downloads.

    ReplyDelete
  15. very nice blogs!!! i have to learning for lot of information for this sites...Sharing for wonderful information.Thanks for sharing this valuable information to our vision. You have posted a trust worthy blog keep sharing.

    Web Designing Course Training in Chennai | Web Designing Course Training in annanagar | Web Designing Course Training in omr | Web Designing Course Training in porur | Web Designing Course Training in tambaram | Web Designing Course Training in velachery

    ReplyDelete
  16. Purchase EMAIL LEADS

    On the off chance that your organization is in the post for speculators or new business tries, business opportunity leads give you data of those needing to begin a business. They are an email away from being a purchaser or your business partner.Time is of the quintessence in each advertising methodology. purchase email lists Regular postal mail, for instance, takes an all-inclusive time from the mission time frame to usage. Not everything organizations can bear to invest such measure of energy to get advertising results. This might be one reason why email showcasing got famous. On account of modernization and high innovation, with your email contact show, you can reach and pass on your message to a huge number of individuals with only a couple clicks away.

    ReplyDelete
  17. Internet Download Manager 6.38 Build 10 IDM Crack Patch with Serial Crucial down load is a sort of shareware downloadmanager possessed by an American company called Tonec Inc.. IDM Patch Free Download captures any type of download in a extraordinary time limit and completes the download at a very brief time and at a faster speed as compared to any download manager right now. IDM company is based in nyc. This computer program is readily designed for Windows OS just for now.
    Presently IDM Serial Crucial Free has turned into a renowned application with over 20 million clients around the globe. Internet download manager Hack may take downloads from many browsers like Internet Explorer, Microsoft Edge, IDM Crack with Serial Key Google Chrome, Opera, Firefox for practically just about any kind of record of user's choice. It has gained control over down load; nevertheless, it shows its dialog box to an individual.

    ReplyDelete
  18. dubai internet directory has business listings, phone numbers, maps, email addresses and websites for local businesses in Dubai to help you get the job do.

    ReplyDelete
  19. Friendly Finance is South Africa's number 1 choice for finance - providing customers with all the information they need to make better financial decisions. We provide hundreds of comparisons of common consumer finance products, apply now such as personal loans, car loans, short-term loans and credit cards. Think of us as a helping hand in selecting consumer finance products.


    ReplyDelete