So I'm having this problem.
I have a simple form:
<form method="post" action="login.php">
<input type="text" name="Username" value="Username" />
<input type="password" name="Password" value="Password" />
<input type="submit" name="submit" value="Log in" />
</form>
And this script:
function focus(element) {
console.log('focused ' + element.getAttribute('name'));
var elementValue = element.value;
var elementName = element.getAttribute('name');
elementValue = (elementValue == elementName ? '' : elementValue);
}
var fields = ['Username', 'Password'];
for (var i = 0; i < fields.length; i++) {
console.log(i + ': ' + fields[i]);
var input = document.getElementsByName(fields[i])[0];
console.log('input value: ' + input.value);
input.onFocus = focus(input);
}
It is supposed to create Focus event listeners for every of the inputs and call focus() function as user clicks on any of the fields. But for some reason, as console shows, it just calls the function once a loop for every of the fields and just stops not responding to future Focus events.
So my guess is there's something wrong with this line input.onFocus = focus(input);
, but I don't know what. What am I missing?