Growly
March 24, 2022, 8:04pm
1
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
Growly
March 25, 2022, 6:41pm
3
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”
rpkamp
March 25, 2022, 6:58pm
4
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?
Growly
March 26, 2022, 7:49pm
7
I figured it out, thank you though
rpkamp
March 26, 2022, 8:24pm
8
Can you share what the solution was, for those reading along?
2 Likes
Growly
March 26, 2022, 11:51pm
9
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
system
Closed
June 26, 2022, 6:51am
10
This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.