I don't understand why this is not working. I'm sticking to the reference on w3schools. Here the code :
HTML:
<button onclick="click()">Test</button>
Javascript:
function click() {
alert("Hello !");
}
I don't understand why this is not working. I'm sticking to the reference on w3schools. Here the code :
HTML:
<button onclick="click()">Test</button>
Javascript:
function click() {
alert("Hello !");
}
It doesn't work because you have configured JSFiddle to wrap the JavaScript in a function and call it onload. This stops your function being a global so it isn't in scope for your intrinsic event handler attribute.
Additionally, after you fix that, the weird scoping rules for intrinsic event attributes (I have no idea where these are documented) means that click is resolved as the button's click property before the scope is searched far enough to find the global click function.
The quick and dirty solution is:
The proper solution is to attach your event handlers with JavaScript. This isn't the 1990s and we should avoid using the techniques of that era that fail to separate concerns. Keep your JS in one place and your HTML in another.
<button>Test</button>
<script>
document.querySelector('button').addEventListener('click', click);
function click(evt) {
alert("Hello !");
}
</script>
no wrap first time i saw this option .. greatclick seems to be reserved.
Try renaming the method: http://jsfiddle.net/qeCE5/1/
function clickMe() {
alert("Hello !");
}