I am trying to use onclick
to call a function
when user click the button, which seems not working.
For example:
function click(){
console.log('you click it!')
}
<input type='button' id='submitbutton' value = 'Submit' onclick= 'click(); console.log("you used it in HTML")'>
console.log
in onclick
, no message will be console
.
function click(){
console.log('you click it!')
}
<input type='button' id='submitbutton' value = 'Submit' onclick= "click();">
The button will console
the message you used it in HTML
which I directly write inside the onclick
, but will fail to execute the function click()
I assign to it.
I think it is not the problem of input[type='button']
since we could use onclick
in input[type='button']
. For example.
Could anyone explain me what makes the code doesn't work? I know the solution for this is probably to use addEventListener
, but why doesn't this work?
Possible duplicate of JavaScript button onclick not working, but this question only suggests the solution not explaining the reason.
Thanks for any responds!