New course! Every coder should learn Generative AI!
Try a free lesson+ 2
here:
$(function(){
var tags = document.getElementsByTagName("eval");
for (var i=0; i<tags.length; i++) {
tags[i].innerHTML = eval(tags[i].innerHTML);
}
});
+ 3
When you getElementsByTagName, you get all elements with that tag name. Invoke each of them using an index.
var evaler = document.getElementsByTagName("eval")[0];
You also want to eval() the contents of the element, not the element itself.
evaler.innerHTML = eval(evaler.innerHTML)
After fixing these two things, your code will display 60.
+ 2
html:
<!DOCTYPE html>
<html>
<head>
<script src="https://code.jquery.com/jquery-3.1.1.js"></script>
<title>Page Title</title>
</head>
<body>
<p id="ev">55+5</p>
</body>
</html>
js:
$(function(){
var evaler = document.getElementById("ev");
evaler.innerHTML = eval(evaler.innerHTML)
});
do you want this?! 😅
+ 2
go throw all elements you get from 'getElementsByTagName' with a for loop and evaluate them