Home > database >  HTML text not changing when button pressed
HTML text not changing when button pressed

Time:04-28

I am trying to make text change to different text when a button is pressed and I'm not sure why it's not working.

<p id="test"> A </p>

<button onclick = "test()"> Click </button>

<script>
function test(){
     document.getElementById("test").innerHTML = "B";
}
</script>

CodePudding user response:

Use innerText insteadof innerHTML like this:

<p id="test"> A </p>

<button onclick = "test()"> Click </button>

<script>
function test(){
     document.getElementById("test").innerText = "B";
}
</script>

CodePudding user response:

const button = document.querySelector('.test__button');

button.addEventListener("click", test)

function test() {
     document.getElementById("test").innerHTML = "B";
}
<p id="test"> A </p>

<button > Click </button>

  • Related