I just started codeing and i need some help reagarding my assignment in javascript

Viewed 16

When left-clicking anywhere on the page, "B" should change to "click"

function change_text(){
var p2 = document.getElementById("p2").value;
document.getElementById("p2").innerHTML = "click";
}
2 Answers

in HTML

<body onload="change_text()">

In your JS file

function change_text(){
  const p2 = document.getElementById("p2");
  document.addEventListener('click', function () {
    p2.innerHTML = 'click'
  })
}

You could use an Listener 'mouseup' (after click) or 'mousedown' (as soon as the button is pressed)

The capture and passive down there will make more faster, it's a garantee that you will not stop normal click events in your function.

    window.addEventListener('mousedown', function (){
        document.getElementById("p2").innerHTML = "click";
    }, {
        capture: true,
        passive: true
    })
Related