You can get the product name in two ways: by querySelector
, or by getElementsByClassName
itself (which you have already tried to use).
Catching querySelector
You can use a query to get the <h1>
that is inside any element that has the class nomeDescricaoProduto
:
document.title = document.querySelector('.nomeDescricaoProduto h1').innerText;
However, querySelector
of JavaScript only returns the first element it finds, that is, it does not return a list, and therefore, would not be appropriate if it had more descriptions to be captured.
So if you want, you can use getElementsByClassName
.
Catching getElementsByClassName
You can also do it the way you have already put it (in response). In your case, it was returning undefined
because you were trying to get the (non-existent) innerText
property of a NodeList , that is, a list of nodes . So if this div is the only one with this class, you can do this:
document.title = document.getElementsByClassName('nomeDescricaoProduto')[0].innerText;
In both cases, if you want, you can execute the trim
method to remove all (perhaps unnecessary) white space 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 have helped!