Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Prevent click on label from triggering child button

When setup like this, clicking on a label that has a child button triggers button's onclick event:

function fireButton() {
  console.log("Button fired!");
}
<label>Label
  <button onclick="fireButton()">Button</button>
</label>

is there a way to prevent this?

like image 577
Guga Figueiredo Avatar asked Nov 03 '25 00:11

Guga Figueiredo


1 Answers

You can add preventDefault for labels and keep the existing code:

document.querySelector("label").addEventListener("click", function(event) { 
    event.preventDefault();
}, false);
like image 63
Brennii96 Avatar answered Nov 04 '25 18:11

Brennii96