Click here to Skip to main content
15,896,348 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!

script.js

$("#button").click(function() {
  $.getJSON('einstein.json', function(read_data) {
    $('examples').html(read_data.result[0].bio);
      });
    });

   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:" + myObj.name + "<br>"+"Birthday:" + myObj.birthday;
        
      };  
};
newRequest.open("GET", "einstein.json", true);
newRequest.send();


index.html
<!DOCTYPE html>
<html>
<head>
    <title>Einstein</title>
    <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
    <script src="script.js"></script>
</head>
<body>
    <div id="example"></div>
    <button id="button">Click Me!</button>
    <div id="examples"></div>
</body>
</html>


einstein.json

{
    "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
Posted
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:
JavaScript
$("#button").click(function() {
    $.getJSON("einstein.json", function(read_data) {
        var name = $("<div/>").text("Name: " + read_data.name);
        var birthday = $("<div/>").text("Birthday: " + read_data.birthday);
        $("#example").append(name).append(birthday);
        $("#examples").text(read_data.bio);
    });
});
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