I'm trying to simply change the text inside all </p>
elements with this code
<html>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap.min.css">
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/js/bootstrap.min.js"></script>
<body>
<p></p>
<script>
elem=document.getElementsByTagName("p");
elem.innerHTML="work";
</script>
</body>
</html>
from this I expect work
to appear on the page, but the result is just a blank page with no text. why?
Assuming you have jquery included you can do it as following:
That way you have a faster cross browser sollution
it returns list of elements
if you have one then you might use:
if you want to apply it to multiple
then you have to iterate over the list
You have to set the index of your HTMLCollection/NodeList.
You can also do it for each
p
tag in your HTML document.It returns a
NodeList
which you need to loop: