Auto-select everything (all text) in forms/search boxes, when focused?
|
|
Can somebody make me a userscript which, when one focuses a form/search box, makes sure that all text already there is selected?
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...
|
|
|
This seems to do the trick... (searchBox = document.getElementById("s")).addEventListener('click', function() { searchBox.value = '' }, false)
|
|
|
|
|
|
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.
|
|
|
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... |
|
|
Put |
|
|
@znerp:
// ==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);
|
