<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
</head>
<body>
<input type="button" id="target1" value="button1" />
<input type="button" id="target2" value="button2" />
<script>
const t1 = document.getElementById("target1");
const t2 = document.getElementById("target2");
function btn_listener(event) {
switch (event.target.id) {
case "target1":
alert(1);
break;
case "target2":
alert(2);
break;
}
}
t1.addEventListener("click", btn_listener);
t2.addEventListener("click", btn_listener);
</script>
</body>
</html>