Which button was pressed?

<button id="b1" value="receive">receive</button>
<button id="b2" value="reject">reject</button>

js

 var el = document.getElementsByTagName("button");
 el.addEventListener("click",do,true);

function do(){
}

when b1 is pressed, the do function alert gives the id value of the pressed button. Excuse me, how to write the do function?

Mar.16,2021

el is an array. You cannot add events directly
Loop arrays add events. In the do function, use this.id to get the id value


add a do is the keyword


Search the key words for such a simple question: "js event" and take a good look at it for an hour.


$("button").on("click",function(){

alert($(this).attr("id"))

})

Menu