.onclick not working

document.querySelector('#mybutton').onclick = function () {
  const h1 = document.querySelector('#myH1')
  h1.innertext = 'bye'
}

<body>


    <h1 id="myH1">Hello</h1>
    <h2 class="myP">This is sentence 1</h2>
    <h2 class="myP">This is sentence 2</h2>
    <button id="mybutton">Click me</button>


    <script src="Htmlelements.js"> </script>
    <script src=""></script>



</body>

The H1 in html is set to “hello”, and this code should switch “hello” to “bye” when clicking button but didn’t work.

JavaScript is case sensitive. innertext needs to be innerText instead.

2 Likes

I have a different problem now

document.querySelector("#mybutton").onclick = function () {
  const h1 = document.querySelector("#myH1");
  h1.innerText = "what is 10 * 32";

<body>


    <h1 id='myH1'>Quiz #1</h1>
    <h2 class="myP">Question</h2>
    <h2 class="myP">Question</h2>
    <h2 class="myP">Question</h2>
    
    <button id='mybutton'>Click me</button>
   


    <script src="Htmlelements.js"> </script>
    <script src=""></script>



</body>

It is still not switching the text “Quiz 1” to “what is 10 *32”

The code posted looks correct. Is the Javascript loaded correctly? Is there any other code that might be messing things up?

I miss a closing bracket in the JavaScript code

Also if the code is before the html it might not work

1 Like

What does the browser console have to say about this different problem?

I figured it out, thank you though

Can you share what the solution was, for those reading along?

2 Likes

There was coding that wasn’t fully complete, and that’s what caused it to not work. and the coding that was there wasn’t put by me, I think I pressed something on accident, and it wrote it lol. keep in mind that I’m a noob

1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.