Reputation: 992
I'm trying to set some text into h1 tag on my website (WordPress Site). I've tested my code in the footer and in the header and it is the last thing to run on the webpage before it is fully loaded.
<script>
function setTitle() {
alert('ok');
var a = document.getElementsByTagName("h1");
a.innerHTML = "yourTextHere";
}
window.onload = setTitle;
</script>
Is anyone able to point out what is wrong with my code or is it something else conflicting with it in WordPress.
Thanks, Luke.
Upvotes: 0
Views: 71
Reputation: 3425
Try this:
Don't use getElementsByTagName(). Instead use getElementsByID and give some ID to your h1 tag where you want to set.
And then Set it like
var a = document.getElementsByID("h1");
a.innerHTML = "yourTextHere";
-
Thanks
Upvotes: 0
Reputation: 17094
getElementsByTagName
returns an HTMLCollection
(an array-like object).
Use
var a = document.getElementsByTagName("h1")[0];
to get the first element of the array.
Upvotes: 1