Last active
August 29, 2015 14:03
-
-
Save cssimsek/4419987fff4cf756ca20 to your computer and use it in GitHub Desktop.
Add text with steps following a particular element
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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