main content, site navigation, search

Selecting Form Field Breaks Backspace Button Functionality

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 shouts to “Selecting Form Field Breaks Backspace Button Functionality”

  1. Vjekoslav
    001—2004.11.17.15:45

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

  2. marko
    002—2004.11.17.17:06

    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.

Comments are closed.

main content, site navigation, search