Skip to content

Instantly share code, notes, and snippets.

@cssimsek
Last active August 29, 2015 14:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cssimsek/4419987fff4cf756ca20 to your computer and use it in GitHub Desktop.
Save cssimsek/4419987fff4cf756ca20 to your computer and use it in GitHub Desktop.
Add text with steps following a particular element
var parElement = document.getElementById("lga"); //get parent element
var containerDiv = document.createElement("div");// create new container div
insertAfter(containerDiv, parElement); //insertAfter function defined below
var myWords = ["Hi", "my", "name", "is", "Paradoxus"]; //array of words to be added
var i = 0;
function addMyWords(i){
if(i < myWords.length){
parElement.nextSibling.textContent += myWords[i] + "\n";
i++;
var timeoutID = window.setTimeout(function(){addMyWords(i)}, 2000);
}
else{
window.clearTimeout(timeoutID);
}
};
addMyWords(i); //initiate addition of words
//insertAfter function from http://snipplr.com/view/2107/insertafter-function-for-the-dom/
function insertAfter(newElement,targetElement) {
//target is what you want it to go after. Look for this elements parent.
var parent = targetElement.parentNode;
//if the parents lastchild is the targetElement...
if(parent.lastchild == targetElement) {
//add the newElement after the target element.
parent.appendChild(newElement);
}
else {
// else the target has siblings, insert the new element between the target and it's next sibling.
parent.insertBefore(newElement, targetElement.nextSibling);
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment