Click here to Skip to main content
15,918,125 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm trying to grab the "bio" from my .json file when the button is clicked on my html file, and have it land in the "examples" div, so that when clicked the "bio" shows up under the "name" and "age". The name and age are working fine, but I can't seem to get the button to grab only the bio. I've only been able to grab the entire array.

What I have tried:

I've tried using the function I'll show you here, and I've tried one more like the one below it as well, but nothing seems to be going through. I'm using vsCode with live server on chrome btw.
Also sorry about the spacing. I'm pretty new if you can't tell!


$("#button").click(function() {
  $.getJSON('einstein.json', function(read_data) {

   let newRequest = new XMLHttpRequest();
      newRequest.onreadystatechange = function() {
        if (this.readyState == 4 && this.status == 200) {
         let myObj = JSON.parse(this.responseText);
          document.getElementById("example").innerHTML ="Name:" + + "<br>"+"Birthday:" + myObj.birthday;
};"GET", "einstein.json", true);

<!DOCTYPE html>
    <script type="text/javascript" src=""></script>
    <script src="script.js"></script>
    <div id="example"></div>
    <button id="button">Click Me!</button>
    <div id="examples"></div>


    "name": "Albert Einstein",
    "birthday": "March 14 1879",
    "bio": "Albert Einstein was a German mathematician and physicist who developed the special and general theories of relativity. In 1921, he won the Nobel Prize for physics for his explanation of the photoelectric effect. ... In his later years, Einstein focused on unified field theory."

Thanks for the help
Updated 17-Mar-20 0:59am

1 solution

Your JSON data is not an array; it's a single object.

You're also missing the # at the start of your jQuery selector.

Try something like this:
$("#button").click(function() {
    $.getJSON("einstein.json", function(read_data) {
        var name = $("<div/>").text("Name: " +;
        var birthday = $("<div/>").text("Birthday: " + read_data.birthday);
NB: I've used .text() instead of .html() / innerHTML to avoid any potential HTML encoding issues, which could potentially lead to cross-site scripting vulnerabilities.

.text() | jQuery API Documentation[^]
.html() | jQuery API Documentation[^]
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900