0

I am trying to make a simple multilanguage site! I have 3 buttons for 3 languages, when I press one of them, I get the value of that button value with jQuery and store it in a variable named clicked and I want to get data from JSON like this: data.clicked.name

How can i get the value of clicked on data.clicked.name?

html file

<div id="container">
    <input class="btnL" type="button" value="MK">
    <input class="btnL" type="button" value="EN">
    <input class="btnL" type="button" value="AL">
  </div>

script

<script type="text/javascript">
    $(document).ready(function () {
      $(".btnL").click(function () {
        var clicked = $(this).attr("value");

        $.getJSON('language.json', function (data) {
          console.log(data.clicked.name);
          // ex. if the value of clicked variable is="EN" 
          //     I want to get this console.log(data.EN.name) 
        });

      });
    });
  </script>

language.json

{
  "EN": {
    "name": "Name",
    "surname": "Surname"
  },
  "AL": {
    "name": "Emri",
    "surname": "Mbiemri"
  },
  "MK": {
    "name": "Име",
    "surname": "Презиме"
  }
}
  • Possible duplicate of [Using variable keys to access values in JavaScript objects](https://stackoverflow.com/questions/922544/using-variable-keys-to-access-values-in-javascript-objects) – Mohammad Oct 27 '18 at 14:45

1 Answers1

4

Use brackets

console.log(data[clicked].name);
Ele
  • 33,468
  • 7
  • 37
  • 75