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.
//#####################################
//## 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;
}
I don't think you can select more then one field at a time.
Focus is also different then select. I think you can only do one or the other. By using select the field should have the focus.
Oh, you've both seemed to misunderstood me, what I'm trying to do is to with Javascript add onFocus="this.select()" to all input type="text" elements on runtime, so that the textboxes automatically select their whole content when they're focused.
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;
}