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": "Презиме"
  }
}
1

1 Answer 1

4

Use brackets

console.log(data[clicked].name);
Sign up to request clarification or add additional context in comments.

1 Comment

Oh damn! I was so close! I tried console.log(data.[clicked].name); but now it works with your solution! Thanks a lot!!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.