Way of doing click event in HTML button.


In this tutorial, we are going to discuss the button click event. 

We can directly call a function on onclick event in HTML tag. 

Let's see the below example. 

1. Directly call event in tag 'onclick'


<button onclick="alert('alert')">Click to alert</button>


Also, We can fire click event by refrencing element.

Let's see the below example.

2. By selecting element

<button id="btn">Click to alert</button>

<script>

  let btn = document.getElementById('btn');

  btn.addEventListener("click",()=>{

    alert("clcked");

})

</script>







Reactions

Post a Comment

0 Comments