I tried to modify this snippet of code found on stackoverflow for different purposes unsuccessful:
<button id="1" onClick="reply_click(this.id)">B1</button>
<button id="2" onClick="reply_click(this.id)">B2</button>
<button id="3" onClick="reply_click(this.id)">B3</button>
<script type="text/javascript">
function reply_click(clicked_id)
{
alert(clicked_id);
}
</script>
The code works great. However, I want to better control my function like something below:
var button1 = document.getElementById('1')
, button2 = document.getElementById('2')
, button3 = document.getElementById('3')
;
function reply_click(clicked_id)
{
console.log(clicked_id);
}
button1.addEventListener("click", reply_click('1'), false);
button2.addEventListener("click", reply_click('2'), false);
button3.addEventListener("click", reply_click('3'), false);
And the console.log on Chrome saying:
Uncaught TypeError: Cannot call method 'addEventListener' of null
How to make this possible?
Thank you.