1

The question is : Convert your object to a JSON string myString and display it in a somewhere on the page. Here is my code:

<!doctype html>
<html>
<head>
  <meta charset="utf-8">
  <title> JSON </title>
  <script>

var fidoString = '{ "name": "Fido", "breed": "Mixed", "weight": 38 }';
var fido = JSON.parse(fidoString);
console.log("We have made a dog out of a string! " + fido.name);


var fido2 = {
    name: "Fido",
    breed: "Mixed",
    weight: 38
};

var fido3 = {
    name: {
        first: "alex",
        second: "doggy"
    },
    breed: "Mixed",
    weight : 30
};
var fidoString = JSON.stringify(fido2);
console.log("We made a string from a dog! " + fidoString);

var x = fido3.name
document.getElementById("lista").innerHTML = x;



  </script>
</head>
<body>
    <div id="lista"></div>
</body>
</html>

I got error: JSON.html:31 Uncaught TypeError: Cannot set property 'innerHTML' of null at JSON.html:31. Can't fix this error and need some help.

user7697691
  • 183
  • 1
  • 8

3 Answers3

0

Your JS will be parsed before the HTML in your body, since the parser reads it top down. And when the code is parsed, the element is not available in the DOM yet.

To solve the issue move the script tag to just before the closing of body tag.

<body>
  <div id="lista"></div>
</body>

<script>
  var fidoString = '{ "name": "Fido", "breed": "Mixed", "weight": 38 }';
  var fido = JSON.parse(fidoString);
  console.log("We have made a dog out of a string! " + fido.name);


  var fido2 = {
    name: "Fido",
    breed: "Mixed",
    weight: 38
  };

  var fido3 = {
    name: {
      first: "alex",
      second: "doggy"
    },
    breed: "Mixed",
    weight: 30
  };
  var fidoString = JSON.stringify(fido2);
  console.log("We made a string from a dog! " + fidoString);

  var x = fido3.name
  document.getElementById("lista").innerHTML = x;
</script>
Sushanth --
  • 53,795
  • 7
  • 57
  • 95
0

You need to move the <script>-tag to the bottom of the <body>-tag. At the moment, the script is executing before the element <div id="lista"> exists. Like this:

<!doctype html>
<html>
<head>
  <meta charset="utf-8">
  <title> JSON </title>
</head>
<body>
    <div id="lista"></div>
    <script type="text/javascript">
    var fidoString = '{ "name": "Fido", "breed": "Mixed", "weight": 38 }';
    var fido = JSON.parse(fidoString);
    console.log("We have made a dog out of a string! " + fido.name);


    var fido2 = {
        name: "Fido",
        breed: "Mixed",
        weight: 38
    };

    var fido3 = {
        name: {
            first: "alex",
            second: "doggy"
        },
        breed: "Mixed",
        weight : 30
    };
    var fidoString = JSON.stringify(fido2);
    console.log("We made a string from a dog! " + fidoString);

    var x = fido3.name
    document.getElementById("lista").innerHTML = x;
    </script>
</body>
</html>

Alternatively wrap the code in a immediately-invoked function, i.e:

(function () {
  // Your code goes here
})()
Sven
  • 4,653
  • 26
  • 49
-1

Put the script at the end of the html.

     <!doctype html>
    <html>
    <head>
      <meta charset="utf-8">

    </head>
    <body>
        <div id="lista"></div>
    </body>
<title> JSON </title>
      <script>

    var fidoString = '{ "name": "Fido", "breed": "Mixed", "weight": 38 }';
    var fido = JSON.parse(fidoString);
    console.log("We have made a dog out of a string! " + fido.name);


    var fido2 = {
        name: "Fido",
        breed: "Mixed",
        weight: 38
    };

    var fido3 = {
        name: {
            first: "alex",
            second: "doggy"
        },
        breed: "Mixed",
        weight : 30
    };
    var fidoString = JSON.stringify(fido2);
    console.log("We made a string from a dog! " + fidoString);

    var x = fido3.name
    document.getElementById("lista").innerHTML = x;



      </script>
    </html>