I don’t get it. Why does this alert on page load? How to make it alert only “onfocus”?
<input id="mail" name="E-post" value="E-post">
<script>
window.onload = function () {
function onfocusFunction() {
alert('Why!?');
}
var myMail = document.getElementById('mail');
myMail.onfocus = onfocusFunction();
}
</script>
With parenthesis
(), you are callingonfocusFunctionfunction and assigningundefinedtomyMail.onfocus:as your
onfocusFunctionfunction does not return anything.Without parenthesis, you will have
onfocusFunctionobject, which should be assigned to.onfocus:UPDATE: If you want to pass parameters either, then try like this:
Then when
onfocusevent will be triggered you will see"value1:value2"as an alert.