0

My code is behaving strangely. Indeed, I created my data in a JSON object:

injectJson = {
   "title": "Questions for a champion",
   "rules": [
      {
        "idChrono": "chrono-minute",
        "text": "Top is gone!",
        "tag": [
            {
                "id": "chronometer",
                "text": "countDown"
            }
        ]
     }
  ]
}

Here is my HTML code:

<div id="rules">
   <h3>The game's rules!</h3>
</div>
<div id="begin">
   <a href="#" class="link">Begin</a>
</div>

And here is the Javascript code with which I inject the HTML code:

selectLinkBegin = document.getElementById("begin");
selectLinkBegin.firstElementChild.addEventListener("click", function(injectJson) {
    // Injection code
    selectRules = document.getElementById("rules");;
    selectRules.firstElementChild.innerText = injectJson.rules[0].texte;
});

In my computer,the JSON object and the JavaScript code are in the same .js file. I load my script at the end of the body tag. When I open my .html code in the browser, the console displays:

Uncaught TypeError: Cannot read property '0' of undefined at HTMLAnchorElement.

But when I copy the Javascript code into the console, I get the expected result: h3's text content is updated.

Demo:

injectJson = {
  "title": "Questions for a champion",
  "rules": [{
    "idChrono": "chrono-minute",
    "text": "Top is gone!",
    "tag": [{
        "id": "chronometer",
        "text": "countDown"
      }

    ]
  }]
}


selectLinkBegin = document.getElementById("begin");
selectLinkBegin.firstElementChild.addEventListener("click", function(injectJson) {
  // Injection code
  selectRules = document.getElementById("rules");;
  selectRules.firstElementChild.innerText = injectJson.rules[0].texte;
});
<div id="rules">
  <h3>The game's rules!</h3>
</div>
<div id="begin">
  <a href="#" class="link">Begin</a>
</div>

Can you explain to me what is going on? How to remedy this? I have already emptied my cache but it's still the same.

3
  • Need to remove the injectJson parameter from your click handler, and use text property instead of texte. Commented Dec 12, 2018 at 7:49
  • Try changing "function(injectJson)" to "function(event)" Commented Dec 12, 2018 at 7:49
  • @RobbyCornelissen Sorry for thsi typo. I translate my code from French Commented Dec 12, 2018 at 7:54

1 Answer 1

2

Your problem is that the variable injectJSON is being hoisted by the param in your click event handler callback function, so it will be undefined that's why you got the error:

Uncaught TypeError: Cannot read property '0' of undefined at HTMLAnchorElement.

In other words the callback param has the same name as your injectJson variable, so that means your original injectJson variable will be undefined, as it was overridden by this param.

You can check the Variable Hoisting section here for further reading about this.

So what you need to do is to remove the callback param:

selectLinkBegin.firstElementChild.addEventListener("click", function() {
  // Injection code
  selectRules = document.getElementById("rules");;
  selectRules.firstElementChild.innerText = injectJson.rules[0].text;
});

Note:

  • Make sure to access the right text element instead of texte in injectJson.rules[0].texte.
  • Make sure to declare your variables with var keyword and avoid using an existing variable name, to avoid such problems.

Demo:

var injectJson = {
  "title": "Questions for a champion",
  "rules": [{
    "idChrono": "chrono-minute",
    "text": "Top is gone!",
    "tag": [{
        "id": "chronometer",
        "text": "countDown"
      }

    ]
  }]
}


selectLinkBegin = document.getElementById("begin");
selectLinkBegin.firstElementChild.addEventListener("click", function() {
  // Injection code
  selectRules = document.getElementById("rules");;
  selectRules.firstElementChild.innerText = injectJson.rules[0].text;
});
<div id="rules">
  <h3>The game's rules!</h3>
</div>
<div id="begin">
  <a href="#" class="link">Begin</a>
</div>

Sign up to request clarification or add additional context in comments.

2 Comments

Got it! Thank you. Can you please explain me what is the callback?
@FEUJIOMarieTherese the callback is the function you pass to the click event handler in: addEventListener("click",function() {...}).

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.