I have the following HTML :
<a class="static selected menu-item" href="/SitePages/Inbox.aspx"><span class="additional-background"><span class="menu-item-text">MyText</span><span class="ms-hidden">Currently selected</span></span></a>
I want to find special text (MyText) in this tag and then change the text.
I use this code but it doesn’t work for me:
var divArray = document.getElementsByTagName("span");
for (var i = 0; i<divArray.length; i++){
if (divArray[i].class="menu-item-text")
return divArray[i].innerHTML;
}
How do I find this with JavaScript and change it?
You can use the querySelectorAll method, which is more widely supported than getElementsByClassName and can be more accurately targeted:
If you know you are looking for a span, then the selector can be
span.menu-item-text. Note that qSA is only supported in IE 8 and 9 in standards mode, so make sure you have a DOCTYPE.Here’s a more robust solution that should work in any browser back to IE 6 or earlier: