DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Snippets has posted 5883 posts at DZone. View Full User Profile

Highlight INPUT Fields OnFocus

02.20.2006
| 762 views |
  • submit to reddit
        // Highlight INPUT fields onFocus

//JavaScript that looks for all elements with the tag name "input" and gives it a class called "textfield_on" when the specific input is in focus
function highlight() {
	var elements = document.getElementsByTagName("input");
    for (i=0; i < elements.length; i++) {
        if (elements[i].type != "button" && elements[i].type != "submit" && elements[i].type != "reset") {
            elements[i].onfocus=function() {this.className='textfield_on';};
            elements[i].onblur=function() {this.className='textfield'};
        }
    }
}

window.onload = highlight;

//example of an input element

<input name="name" id="name" type="text" class="textfield" />