My goal is to implement an automatic page translation feature using Google Translate's Library. I have been following the instructions provided in this link:
The issue lies in the code snippet below, where the select menu creation works fine, but accessing it to change the language selection seems to be problematic.
<script src="//translate.google.com/translate_a/element.js?cb=googleTranslateElementInit"></script>
<div id="google_translate_element"></div>
<div class=to-translate>text to be translated</div>
<div class="notranslate">this text will not be translated</div>
<script>
function googleTranslateElementInit() {
new google.translate.TranslateElement(
{
pageLanguage: "en"
},
"google_translate_element"
);
}
var lang = "fr";
const select = document.getElementsByClassName('goog-te-combo');
console.log(select, select[0], select.length);
select[0].value = lang;
After trying different methods, including the provided log statement, I encountered difficulties with indexing the HTML Collection correctly. Any suggestions on how to resolve this issue would be greatly appreciated. Thank you in advance!