I am trying to find placeholder text (A placeholder looks like this: {{Placeholder}}
) within a document, then get the node that it is apart of, then add an attribute to the node. So far I am able to find the placeholder but I don't know which element it is apart of. The current code writes I am here to the console 3 times because it is in 3 nodes: html
, body
and h1
. How can it get the items actual parent node?
JavaScript:
function getPlaceholders(){
var elements = document.getElementsByTagName("*");
for(var e in elements){
var element = elements[e];
var html = element.innerHTML;
if(html && (matches = html.match(/{{(.+)}}/g))){
console.log("I am here");
}
}
}
HTML:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
</head>
<body>
<h1>Hello {{name}}!</h1>
</body>
</html>