Created
September 29, 2013 19:10
-
-
Save stubbornella/6755569 to your computer and use it in GitHub Desktop.
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
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta charset=utf-8 /> | |
<title>JS Bin</title> | |
</head> | |
<body> | |
<div class="text">text to be replaced</div> | |
</body> | |
</html> |
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 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