Page 1 of 1

Add onFocus select() to all texboxes in document

Posted: Sat Jul 23, 2005 7:44 am
by vigge89
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.

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&#1111;i].type == 'text')
			inputFields&#1111;i].onFocus = &quote;this.select()&quote;;
	}

	// set focus to the first field
	inputFields&#1111;0].focus();
	return true;
}
Thanks in advance // vigge

Posted: Tue Jul 26, 2005 11:48 am
by vigge89
Anyone? :(

Posted: Tue Jul 26, 2005 3:18 pm
by phpScott
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.

Posted: Tue Jul 26, 2005 3:51 pm
by Chris Corbyn
phpScott wrote:I don't think you can select more then one field at a time.

Code: Select all

<select type=&quote;multiple&quote;></select>
;)

Try onchange ;)

Posted: Tue Jul 26, 2005 4:26 pm
by vigge89
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.

Posted: Wed Jul 27, 2005 6:13 am
by Chris Corbyn
Ah we both misunderstood you and added to that I misunderstood phpScott lol :P

I don't understand... is it not working?

Code: Select all

<input type=&quote;text&quote; onfocus=&quote;this.select()&quote; />
Tested it and it works ;)

Posted: Wed Jul 27, 2005 6:27 am
by Chris Corbyn

Code: Select all

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&#1111;i].type == 'text')
			inputFields&#1111;i].onfocus = &quote;this.select()&quote;;
	}
 
	// set focus to the first field
	inputFields&#1111;0].focus();
	return true;
}
Ah sorry I see.... use lowercase onfocus ;)

Posted: Wed Jul 27, 2005 1:31 pm
by vigge89
Won't work =/

However, I found a way to make it work in Firefox, but only in FF from what I know;

Code: Select all

inputFields&#1111;i].addEventListener(&quote;focus&quote;, function() { this.select(); }, false);
See it in action

I'll continue looking for something with cross-browser compatibilty, if anyone finds anything, I'd be happy if you posted it here.