Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Auto select text

Status
Not open for further replies.

FancyPrairie

Programmer
Oct 16, 2001
2,917
US
When the user enters a field (input box or textarea) I want the text to automatically be selected. I know that it can be done via the onfocus event of the control (i.e. document.all(window.event.srcElement.uniqueid).select). However, I don't want to have to add onfocus=... to the definition of every input box. Is there a document event or something that gets fired each time a element receives focus?

I've tried using the onfocus event of the document (document_onfocus), but that never seems to fire off. I've also tried the onfocusin event of the document. Although that fires each time a field receives focus, the fields are not selected. For example, the following code does not work in the onfocusin event.
Code:
sub document_onfocusin()
	document.all(window.event.srcElement.uniqueid).select
end sub
 
Use a javascript function to grab all the input elements and apply a behaviour to them with a simple Javascript function.

You'll get a better answer in the Javascript Forum (Forum216) but something like this might work

Code:
function prepInputs(){
    inputs = document.getElementByTagName('INPUT');

    for(i=0;i<inputs.length;i++){
        inputs[i].onfocus = function(){
           //do the thing that selects the content here
        }
    }
}


window.onload=prepInputs();

*no guarantees it will work, but that's the general idea

<honk>*:O)</honk>
Foamcow Heavy Industries - Web site design in Cheltenham and Gloucester
Ham and Jam - British & Commonwealth forces mod for Half Life 2
 
If I can get your concept to work, it would open the door to a lot of things for me. But, I get the error "Object doesn't support this method or property" on this line:

inputs = document.getElementByTagName('INPUT');
 
You're right about the name, but I still can't seem to get it to work.

I tried this:
inputs.onfocus = function(){this.select}

and this:
inputs.onfocus = function(){document.all(window.event.srcElement.uniqueid).select }

and this:
inputs.onfocus = function(){ATest();}

Any ideas?
 
This is really a Javascript thing so should be discussed in the JS forum (Forum216)

But as a last post.... it works perfectly (in FF, not tried IE, but it should be fine) like this

Code:
<script type="text/javascript" charset="utf-8">
	function prepInputs(){
		var inputs = document.getElementsByTagName('INPUT');

		for(i=0;i<inputs.length;i++){
			inputs[i].onfocus = function(){
				this.select();
			}
		}
	}

	window.onload=prepInputs;
</script>

<honk>*:O)</honk>
Foamcow Heavy Industries - Web site design in Cheltenham and Gloucester
Ham and Jam - British & Commonwealth forces mod for Half Life 2
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top