0

I have a page with a dropdown list and a link tag.

I am unable to modify the html on the page at all but I can add javascript to the page.

I need to be able to translate the text of the .goButton element when a user selects a different language option from a dropdown list.

How can I accomplish this with javascript alone?

<a class="goButton" ... >Send</a>

<select name="ddlLanguages" id="ddlLanguages" class="form-control">
    <option value="zh-CN">Chinese (Simplified, PRC)</option>
    <option selected="selected" value="en-GB">English (United Kingdom)</option>
    <option value="en-US">English (United States)</option>
    <option value="fr-FR">French (France)</option>
    <option value="de-DE">German (Germany)</option>
    <option value="it-IT">Italian (Italy)</option>
    <option value="pt-BR">Portuguese (Brazil)</option>
    <option value="es-ES">Spanish (Spain)</option>
</select>
jessegavin
  • 74,067
  • 28
  • 136
  • 164
sbouaked
  • 955
  • 10
  • 29

4 Answers4

3

Try it this way, you can store the text for each language in the option element:

document.getElementById('ddlLanguages').addEventListener('change', function (elem) {
    document.querySelector("a.goButton").innerHTML =
        this.options[this.selectedIndex].getAttribute('data-action');
});
<select name="ddlLanguages" id="ddlLanguages" class="form-control">
  <option data-action='发送' value="zh-CN">Chinese (Simplified, PRC)</option>
  <option data-action='BritishSend' elected="selected" value="en-GB">English (United Kingdom)</option>
  <option selected data-action="AmericanSend" value="en-US">English (United States)</option>
  <option data-action="Envoyer" value="fr-FR">French (France)</option>
  <option data-action="Senden" value="de-DE">German (Germany)</option>
  <option data-action="Inviare"  value="it-IT">Italian (Italy)</option>
  <option data-action="Enviar" value="pt-BR">Portuguese (Brazil)</option>
  <option data-action="Enviar" value="es-ES">Spanish (Spain)</option>

</select>
<a onclick="fillSearch()" class="goButton">AmericanSend</a>
Ruan Mendes
  • 90,375
  • 31
  • 153
  • 217
Rayon
  • 36,219
  • 4
  • 49
  • 76
  • @JuanMendes, for that he need to maintain reference with the value selected, He has not provided it anywhere in his question.. – Rayon Sep 18 '15 at 12:58
  • @JuanMendes, I have updated my answer. Thanks for the efforts ! – Rayon Sep 18 '15 at 13:10
2

If you're trying to translate that button

JAVASCRIPT

function changeLanguage(e) {
  var lang = e.target.options[e.target.selectedIndex].value;

  var textMap = {
    "en-US" : "send",
    "en-GB" : "send",
    "fr-FR" : "envoyer",
    "zh-CN" : "发送",
    "de-DE" : "senden",
    "de-DE" : "senden",
    "it-IT" : "inviare",
    "pt-BR" : "mandar",
    "es-ES" : "enviar"
  };


  if (lang && textMap[lang]) {
    document.querySelector(".goButton").innerText = textMap[lang];
  }
}

document.getElementById("ddlLanguages").addEventListener("change", changeLanguage);

HTML

<select name="ddlLanguages" id="ddlLanguages" class="form-control">
    <option value="zh-CN">Chinese (Simplified, PRC)</option>
    <option selected="selected" value="en-GB">English (United Kingdom)</option>
    <option value="en-US">English (United States)</option>
    <option value="fr-FR">French (France)</option>
    <option value="de-DE">German (Germany)</option>
    <option value="it-IT">Italian (Italy)</option>
    <option value="pt-BR">Portuguese (Brazil)</option>
    <option value="es-ES">Spanish (Spain)</option>

</select>

DEMO HERE

jessegavin
  • 74,067
  • 28
  • 136
  • 164
  • this is exactly what I want to do, but (and I m sorry, i forgot to explain) I can't edit the HTML, i can just add some JS, so i can't add the onchange value to use your function. – sbouaked Sep 18 '15 at 13:01
  • 2
    @sbouaked You can register a change handler from JavaScript instead of through the HTML attribute – Ruan Mendes Sep 18 '15 at 13:07
  • 2
    Consider using http://stackoverflow.com/a/32652589/227299 to store the language in each of the options instead of requiring a map where the property names have to match up with the text encoding – Ruan Mendes Sep 18 '15 at 13:16
  • @JuanMendes That was actually going to be the next thing I was going to recommend, but you beat me to it. – jessegavin Sep 18 '15 at 13:21
  • @JuanMendes i'm agree but in this case I have no control over the html ;) – sbouaked Sep 18 '15 at 18:02
1
var a = document.querySelector('.goButton');
a.textContent = 'Changed';
Lee
  • 2,993
  • 3
  • 21
  • 27
1

Use a query selector

document.querySelector(".goButton").innerHTML="Send 2";

This queries the elements based on CSS selectors

Documentation

https://developer.mozilla.org/en-US/docs/Web/API/Document/querySelector

cjds
  • 8,268
  • 10
  • 49
  • 84