insertAfter Function


/ Published in: JavaScript
Save to your folder(s)

Description: A function to insert a new element after an existed one.
Arguments: The new element and the target element the
new element should be inserted after.


Copy this code and paste it in your HTML
  1. function insertAfter(newElement,targetElement) {
  2. var parent = targetElement.parentNode;
  3. if (parent.lastChild == targetElement) {
  4. parent.appendChild(newElement);
  5. } else {
  6. parent.insertBefore(newElement,targetElement.nextSibling);
  7. }
  8. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.