Mayoco Mayoco - 5 months ago 6
HTML Question

onclick event handler not work

This is a example code in JavaScript: The Definitive Guide,6th Edition.

<button id="my button">click me</button>
<script>
var b = document.getElementById("my button");
b.onclick = fuction(){alert("Thanks for clicking me!");}; /*work well if I delete this parse*/
b.addEventListener ("click", function(){ alert("Thanks again!");}, false);
</script>


When I click the button, nothing happen.

Answer

are you sure you write well?

1 - Remove space on id value: change id="my button" to id="mybutton".

2 - is not fuction, is function.

<button id="mybutton">click me</button>
<script>
var b = document.getElementById("mybutton");
b.onclick = function(){alert("Thanks for clicking me!");}; /*work well if I delete this parse*/
b.addEventListener ("click", function(){ alert("Thanks again!");}, false);
</script>

Comments