If you are reading this, please get modern browser.
skip to main content | skip to main navigation | skip to secondary content

Selecting Form Field Breaks Backspace Button Functionality

~ 17th November 2004. · 12:20 CET · permanent link · printer friendly ~

Until yesterday, there was JavaScript function on this web site, which selected the first form input field when page loads. The same functionality is implemented at Google homepage. Some users don’t use mouse for basic browsing (such as going back to previous page), it’s discovered that if input field is focused or selected, there’s no way to go back by pressing backspace. So i removed it this morning, but left some functionality. When user focuses input field by himself, the script selects default input field value (not just placing cursor after it), so the user doesn’t have to delete it.

Onfocus—Select

function selectWhenFocused() {
    // checks if there's form element in the document:
    if (document.forms) {
        // loops through all forms in the document:
        for (var i = 0; i < document.forms.length; i++) {
            var forma = document.forms[i];
            var eles = forma.elements;
            // loops through elements of the form:
            for (var j = 0; j < eles.length; j++) {
                if (eles[j].type == 'text') {
                    eles[j].onfocus = function () {
                        this.select();
                    }
                }
            }
        }
    }
}
window.onload = function() {
	selectWhenFocused();
        // place here all other functions you want to call when page loads
}

Have a nice day!

2 Comments

  1. You don’t have “if (document.getElementsByTagName)” before using that method. Some older browser will report an error.

  2. Thanks Vjekoslav for your remark. The script is updated and it should be working without errors.

    Zytzagoo suggested use of elements[] collection which returns an array containing each element in the form. This should work even in IE 3.

Sorry, the comment form is closed at this time, but if you have anything to say, please send me a message.

* Please keep in mind that this is a personal web site and it does not reflect the position or opinion of my respective employers, organizations or partners.

Typetester – compare screen type Supported by Veer.

What is this?

A web log of Marko Dugonjić, web professional from Croatia. Topics covered:

Translate this site

German, Spanish, Italian, French or Japanese (via).

See you there!

Feel like buying a book?

Try with maratz.com aStore

Worth visiting

top of the page | skip to main content | skip to main navigation | skip to secondary content