0

I have a function written in JavaScript. The function is correct, I cannot change it. I need to write HTML code to handle it. Unfortunately, I do not fully understand how it works, when starting the debugger it screams errors:

function A2(params) {
  if ((params / (10 % 4)) === 132.993) {
    var m = document.querySelector("Math");
    console.log("A2 hip hip hura");
    m.innerHTML = "Set Text In Here :";
    var arr = [m];
    arr.push("Element");
    arr.push("Second Element");
    m.addEventListener("click", A2(arr));
  }
}
<div id="Math">
  <input type="text" value="nothing" />
</div>

<input type="submit" value="SEND" onclick="A2(265.986)" />

error:
TypeError: Cannot set properties of null (setting 'innerHTML')

10
  • 2
    try var m = document.querySelector("#Math"); (note the # which refer to an id) Commented Feb 23, 2022 at 8:16
  • 1
    Your queryselector is missing a "#" infront of "Math" and therefore returning undefined. Adding a hashtag should do the trick. You should have a selector like "#Math". Commented Feb 23, 2022 at 8:17
  • 2
    If you cannot change the Javascript, there's no way to make it work using whatever HTML you come up with. Commented Feb 23, 2022 at 8:23
  • 1
    Additionally, even if the selector will be fixed, the line m.addEventListener("click", A2(arr)); won't work, nothing will happen when m is clicked, since A2 doesn't return a function. Commented Feb 23, 2022 at 8:31
  • 1
    Go back to whomever told you it was correct and tell them you've found a few errors. It is possible to add a Math element <Math ...></Math> to your HTML but this is unlikely and not expected. Commented Feb 23, 2022 at 8:35

2 Answers 2

3

you need to use the querySelector according to the right selector which is # in that case

<html>  
<head>  
<script type = "text/javascript">  
function A2(params) {
    if ((params / (10 % 4)) === 132.993) {
        var m = document.querySelector("#Math");
        console.log(m)
        console.log("A2 hip hip hura");
        m.innerHTML = "Set Text In Here :";
        var arr = [m];
        arr.push("Element");
        arr.push("Second Element");
        m.addEventListener("click", A2(arr));
    }
}
</script>  
</head>  
<body>  
<div id="Math">
    <input type="text" value="nothing" />
</div>

<input type="submit" value="SEND" onclick="A2(265.986)"/>
</body>  
</html> 

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

3 Comments

Nothing happens when you click "Set Text In Here :" text.
This seems to work for me. @Teemu perhaps Firefox's issue with "Math" goes beyond element names!
@phuzi Maybe you've misunderstood my comment, I meant that the event listener attached inside the function doesn't work.
0

What you forgot is just # sign before Math. Use var m = document.querySelector("#Math"); instead of var m = document.querySelector("Math"); In addition, be sure to read these:

Document.querySelector()

CSS Selector Reference

Here, a little example, just run code snippet:

function A2(params) {
    if ((params / (10 % 4)) === 132.993) {
        var m = document.querySelector("#Math");
        console.log("Responding!");
        m.innerHTML = "Button is pressed:";
    }
}
.button {
  background-color: #04AA6D;
  border: none;
  color: white;
  padding: 20px;
  text-align: center;
  text-decoration: none;
  display: inline-block;
  font-size: 16px;
  margin: 4px 2px;
}

.button {border-radius: 5px;}
<!DOCTYPE html>
<html>
<head>
<style>
.button {
  background-color: #4CAF50;
  border: none;
  color: white;
  padding: 15px 32px;
  text-align: center;
  text-decoration: none;
  display: inline-block;
  font-size: 16px;
  margin: 4px 2px;
  cursor: pointer;
}
</style>
</head>
<body>

<div id="Math">
    <input type="text" value="nothing" />
</div>

<input type="submit" class="button" value="SEND" onclick="A2(265.986)">

</body>
</html>

4 Comments

Nothing happens when you click "Button is pressed:" text.
@Teemu, sorry, does the question cover that as well?
There's no question in the post, but in general you should fix all the errors if you're going to answer.
@Teemu, you're totally right! Unless he edits the question, I'm just removing that part of the code.

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.