Ajit N

Ajit N

  • 1.7k
  • 339
  • 27.9k

How to pass function with argument in addEventListener ?

Mar 9 2021 6:18 AM
I want to execute a function with argument from addEventListener on button click. i wrote some code,but here function is executing before button click.....below sample code....
  1. <form>  
  2.    <button id="btn">Click Here</button>  
  3.        </form>  
  4.    <script>  
  5.          
  6.        function abc(num) {  
  7.            alert(num);  
  8.        };  

  9.        document.getElementById('btn').addEventListener('click', abc(2));  
  10.    </script>  
help me how to solve this problem... 

Answers (2)