Weird error in console [HTML]

Question: what do i do about this wierd error?

the error randomly appeared in console and I have no idea what’s causing it. because thesite is treating the line of code as if it were a function, I cannot run my project.


image


this is the only line of code I have that utilizes the

document.getElementById(...).innerHTML(...)

code.


line of code:

        document.getElementById("p1score").innerHTML("P1 Score: " + p1score);

edit:
just to be clear “p1score” is used in a div tag and is also a variable name.

If you want to set the text inside #p1score, it’s:

document.getElementById("p1score").innerText = `P1 Score: ${p1score}`

If you rename your current p1score variable to, say, score , you can make this just

p1score.innerText = `P1 Score: ${score}`
2 Likes

In the .innerHTML(…) you need to use .innerHTML = “P1 Score: " + p1score”;

document.getElementById("p1score").innerHTML = "P1 Score: " + p1score;

Beat me to it man :confused:

Actually, .textContent is better than innerText since it exists for Nodes as well as Elements, and it doesn’t need to read the layout of the page.

And @anon45021817, innerHTML is usually a bad choice as it often allows for XSS attacks.

4 Likes

Good point. I use it because I don’t deal with any high security stuff. For personal projects (like a game), I use innerHTML because that’s nature for me. This guy was probably making a game judging by his variable names, but thanks for the tip!

1 Like

:+1: but there’s no difference between setting innerText or textContent.
I use innerHTML when necessary for data that I receive from the backend.
But @anon45021817 do note that textContent is also usually faster than innerHTML because it doesn’t have to parse

2 Likes

True. innerText would work for me because the projects I work with use a lot of input fields, so that would help because it doesn’t parse everything.

1 Like

@anon45021817 @LuisAFK @UMARismyname Please keep discussion on topic and related to this topic. If you wish to discuss more please make a topic in #general or message each other.

1 Like

Got it, I think we answered the OP’s question enough.

1 Like