Add onFocus select() to all texboxes in document
Posted: Sat Jul 23, 2005 7:44 am
I'm trying to by javascript add an action on the event onFocus which automatically selects the contents of the textbox focused, but I've come to a problem, the action added doesn't do anything. I need to know what I should use as the action to make it work.
Here's my current code, in which the lines 25-28 contains the code for this.
Thanks in advance // vigge
Here's my current code, in which the lines 25-28 contains the code for this.
Code: Select all
//#####################################
//## script.js
//## JS: AUTOFOCUS FIRST INPUT FIELD
//#####################################
// add controlInputs() call to the onload function
var oldOnload = window.onload;
if (typeof (window.onload) != 'function') {
window.onload = controlInputs;
} else {
window.onload = function() {
oldOnload();
controlInputs();
}
}
function controlInputs() {
// create hooks to all input fields
var inputFields = document.getElementsByTagName('input');
// exit if no input fields could be found
if (inputFields.length < 1)
return false;
// add onFocus to select the content of the field
for (var i = 0; i < inputFields.length; i++) {
if (inputFieldsїi].type == 'text')
inputFieldsїi].onFocus = "e;this.select()"e;;
}
// set focus to the first field
inputFieldsї0].focus();
return true;
}