Alex Rivera | Logout

Change Google Translate dropdown programmatically

Asked 2011-04-06T16:39:05.023
11

On a site I tried adding the Google Translate dropdown using the following code:

function googleTranslateElementInit() {
  new google.translate.TranslateElement({
    pageLanguage: 'en'
  }, 'google_translate_element');
}

<script src="http://translate.google.com/translate_a/element.js?cb=googleTranslateElementInit"></script>

When you select from the dropdown that the google script inserts, a Google Translate bar appears at the top of the page, and all text is translated in to the selected language.

However if I try and trigger the dropdown change using JavaScript, it doesn't work:

$('.goog-te-combo').val('fr')

'French' is selected from the dropdown, however Google Translate is not triggered.

Why o why does it not work? I've also tried:

$('.goog-te-combo').trigger('click')
$('.goog-te-combo').change()

UPDATE: FYI this is not my site. I was using the Chrome console to load jQuery and execute the jQuery methods.

Edit
Report

1 Answer

2

In looking at your page it appears that jQuery isn't loaded, so you won't be able to use the $() function.

You need to add a reference to jQuery in your <head></head> section, such as:

<script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js"></script> 

Then

$('.goog-te-combo').val('fr');

should work.

answered 2011-04-06T16:59:34.273

Your Answer