Auto-select everything (all text) in forms/search boxes, when focused?

Subscribe to Auto-select everything (all text) in forms/search boxes, when focused? 7 posts, 3 voices

 
FF3user User

Can somebody make me a userscript which, when one focuses a form/search box, makes sure that all text already there is selected?
Even though javascript is disabled for the site (I use NoScript add-on)?

Testpage: http://5440.morelyrics.co.uk/index.php?s=firefox (with NoScript/javascript disabled for that site, clicking in the search field, the "Search..." part remains present)

Would be nice to have such a script, as one wouldn't have to press "SHIFT+HOME" or select everything with the mouse, before he/she could type the wanted query into it...
Thanks in advance.

 
znerp Scriptwright

This seems to do the trick...

(searchBox = document.getElementById("s")).addEventListener('click', function() { searchBox.value = '' },  false)

 
dob Scriptwright

searchBox.addEventListener("focus", function() {
	this.selectionStart = 0;
	this.selectionEnd = this.value.length;
}, false);

 
znerp Scriptwright

Yeah, that would do it too. I was just copying the existing behaviour from the site...

<input type="text" onblur="if (this.value == '') {this.value = 'Search...';}" onfocus="if (this.value == 'Search...') {this.value = '';}" id="s" name="s" value="Search..."/>
Althought thinking about it a second time, I should really have used the focus event.

 
FF3user User

Thanks for the quick replies, but the suggestions doesn't seem to work for me on the test site with javascript disabled.

Maybe (if you don't have NoScript) you could try having javascript disabled via this link: http://tinyurl.com/6nled5 (testpage seen through vTunnel with javascript choosen disabled)

The code I am using (I have tried both of them):

// ==UserScript==
// @name           Auto-select all text in forms/search boxes when focused
// @namespace      http://userscripts.org/forums/2/topics/2779
// @description    Auto-select all text in forms/search boxes when focused
// @include        *
// ==/UserScript==

searchBox.addEventListener("focus", function() {
	this.selectionStart = 0;
	this.selectionEnd = this.value.length;
}, false);

P.S. all the text is choosen with/without the script if I focus the box using the "Tab" key. Same it is if I tripple click in the box...

 
znerp Scriptwright

Put searchBox = document.getElementById("s") at the top of that code.

 
FF3user User

@znerp:
Thanks, it now works if I click behind/after/to the right of the "Search..." text, but if I click on the text/in the middle of the letters, sometimes often it just blinks and afterward my text cursor is placed between two of the letters... Can this be fixed?
(If not this is still better than before...)
My code with your addition:

// ==UserScript==
// @name           Auto-select all text in forms/search boxes when focused
// @namespace      http://userscripts.org/forums/2/topics/2779
// @description    Auto-select all text in forms/search boxes when focused
// @include        *
// ==/UserScript==

searchBox = document.getElementById("s")
searchBox.addEventListener("focus", function() {
	this.selectionStart = 0;
	this.selectionEnd = this.value.length;
}, false);