i tried to change this text using getElementsByTagName() but it did not work i do not know what is the probleme
getElementsByTagName()
i tried to change this text using getElementsByTagName() but it did not work i do not know what is the probleme
getElementsByTagName()
2
Answers
You should check the DOM documentation because you are misunderstanding what that function does,
getElementsByTagName("s-button-text")
isn’t getting the element because thats not how it works.This would work
getElementsByClassName("s-button-text")
orgetElementsByTagName("span")
, tag refers to the<span>
in this case, and class refers to theclass="
attribute.I would highly recommend you don’t use the second one as it will get other
<span>
elements in the page and not just the one you want.As well, even if you replace that it will create an error, this is how to do what you want to do:
getElementsByTagName()
returns an array of elements by tag name. You are trying to get an element by it’s class so you need a different method. You can usequerySelector()
orquerySelectorAll()
.querySelector()
is used to find a single element by a CSS selector.querySelectorAll()
is used to get a list of elements by a CSS selector.This will find all elements with this class name and change the text of the first element returned: