Recommand · May 23, 2021 0

Simple Javascript Event Handler Not Working Properly:

The code below should log a statement to the page console upon clicking a paragraph on the page. I can’t seem to figure out why it isn’t working. Can anyone help me?

function subsupPClicked() {
    console.log('You clicked the "subsupP" paragraph.');
}
document.getElementById("subsupP").onclick = subsupPClicked;

You can try this instead:

document.getElementById("subsupP").addEventListener("click", subsupPClicked)

function subsupPClicked() {
    console.log('You clicked the "subsupP" paragraph.');
}