You can take the product name in two ways: by querySelector
, or by himself getElementsByClassName
(which you have tried to use).
Fur-picking querySelector
You can use a query to get the <h1>
which is within any element possessing the class nomeDescricaoProduto
:
document.title = document.querySelector('.nomeDescricaoProduto h1').innerText;
However, the querySelector
Javascript only returns the first element he thinks, that is, it does not return a list, and therefore, it would not be appropriate if it had more descriptions to be captured.
So if you wish, you can use the getElementsByClassName
.
Fur-picking getElementsByClassName
You can also do as you have already put (as a response). In your case was returning undefined
, because, you were trying to take the property (non-existent) innerText
of a Nodelist, that is, a nod list. So if that div is the only one with this class, you can do it this way:
document.title = document.getElementsByClassName('nomeDescricaoProduto')[0].innerText;
In both cases, if you want, you can run the method trim
to remove all whitespace (perhaps unnecessary) from the front and back of the string:
document.title = document.querySelector('.nomeDescricaoProduto h1').innerText.trim();
Or
document.title = document.getElementsByClassName('nomeDescricaoProduto')[0].innerText.trim();
I hope I’ve helped!
document.title = document.getElementsByClassName('nomeDescricaoProduto')[0].innerText.trim();
Just what I wanted to see– Walyson Patric