Skip to content

Instantly share code, notes, and snippets.

@stubbornella
Created September 29, 2013 19:10
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 stubbornella/6755569 to your computer and use it in GitHub Desktop.
Save stubbornella/6755569 to your computer and use it in GitHub Desktop.
<!DOCTYPE html>
<html>
<head>
<meta charset=utf-8 />
<title>JS Bin</title>
</head>
<body>
<div class="text">text to be replaced</div>
</body>
</html>
var text,
content = "<div>a</div><div>e</div><div>t</div><div>7</div><div>1</div>";
text = document.getElementsByClassName("text"); // this could run before the document is ready... fix.
for (i=0; i< text.length; i++){
text[i].innerHTML = content;
}
registerEventHandler(document.body, "click", function(){
alert(findIndex(event.target));
});
function findIndex(node) {
var siblings,
index = -1;
siblings = node.parentNode.children;
for (i=0; i<siblings.length; i++){
index ++;
if (siblings[i] == node) {
return index;
}
}
return -1;
}
function registerEventHandler(node, event, handler){
if (typeof node.addEventListener == "function") {
node.addEventListener(event, handler, false);
}
else {
node.attachEvent("on" + event, handler);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment