Page 1 of 1

Clean input text fields not working in IE6

Posted: Thu Feb 23, 2012 1:51 pm
by FernandoBasso
This is my code so far. It should handle all input text fields on a page. It works in opera, firefox, and chrome, all in their latest versions, except in IE6, which is the only IE version I have installed. My OS is arch linux, and I have IE6 installed in wine.

When I run/open the page, I don't see any errors in console (firebug) and IE doesn't complain about anything either, but when I give focus or blur, IE simply doesn't respond, and the text remains there.

Code: Select all

window.onload = function() {

    /* Gets all <input> elements. */
    var inputs = document.getElementsByTagName('input');

    for (var i = 0; i < inputs['length']; i++) {

        if (inputs[i].type == 'text') {

            /* This proves that it is undertading something at least. */
            //alert(inputs[i].value);

            /* Stores the default text value in a new property (variable),
             * no matter what browser is used.. */
            inputs[i].defaultText = inputs[i].value;

            /* W3C: Add event listeners only to text input fields. */
            if (typeof inputs[i].addEventListener != 'undefined') {
                inputs[i].addEventListener('focus', clearDefault, false);
                inputs[i].addEventListener('blur', checkEmpty, false);
            }
            /* IE: Add attach events to toext input fields. */
            else if (typeof inputs[i].attachEvent != 'undefined') {
                inputs[i].attachEvent('onfocus', clearDefault);
                inputs[i].attachEvent('onblur', checkEmpty);
            }

        } // Outer if ends here.
    } // for ends here.

    function clearDefault() {
        /* Clears -only- if the the value is
         * equal the initial default text. */
        if (this.value == this.defaultText) {
            this.value = '';
        }
    }

    function checkEmpty() {
        /* Places back the default text if the input field is emtpy. */
        if(this.value == '') {
            this.value = this.defaultText;
        }
    }
}
The html has a simple form. This piece of js dosen't relly on IDs, so any form will do (for testing purposes).

Any help would be appreciated.