
Originally Posted by
Black Max
what's the right way to accomplish this, can I tighten up this code to be clean and tidy
You can either use a simple bit of code like this:
Code:
value="search amy's site" onfocus="if(this.value == 'search amy's site'){this.value = '';}"
or use a proper script that is unobtrusive (this one courtesy of Jeremy Keith):
Code:
function addLoadEvent(func) {
var oldonload = window.onload;
if (typeof window.onload != 'function') {
window.onload = func;
} else {
window.onload = function() {
oldonload();
func();
}
}
}
function resetFields(whichform) {
for (var i=0; i<whichform.elements.length; i++) {
var element = whichform.elements[i];
if (element.type == "submit") continue;
if (!element.defaultValue) continue;
element.onfocus = function() {
if (this.value == this.defaultValue) {
this.value = "";
}
}
element.onblur = function() {
if (this.value == "") {
this.value = this.defaultValue;
}
}
}
}
function prepareForms() {
for (var i=0; i<document.forms.length; i++) {
var thisform = document.forms[i];
resetFields(thisform);
thisform.onsubmit = function() {
return validateForm(this);
}
}
}
addLoadEvent(prepareForms);
and most importantly, can someone explain it to me so I can understand it?
Unfortunately not! I'm gradually getting my head around it, but I recommend Jeremy Keith's DOM Scripting book if you really want a proper explanation.
Bookmarks