The onFocus event keeps firing on page load and doesn’t seem to work when the element goes into focus. I only want the alert to fire off when the the input comes into focus not on page load
//Input
var input = document.getElementById('phonenumber');
//onfocus execute function
input.onFocus = alert('test')
You’re calling the function
alertand assigning its return value (undefined) to the focus handler. Try this instead:Or, perhaps more understandably:
Note that there are no parentheses after
inputFocusedin the assignment. We want to setonfocusto the function itself, not to the result of calling the function.