Code:
window.focussedFormElement = null;
function addStuff() {
var newtext = 'New Stuff';
window.focussedFormElement.value += newtext;
}
window.onload = function() {
var n = function() {
window.focussedFormElement = this.form.focussedElement = this;
}, p = function() {
window.focussedFormElement = this.form.focussedElement = null;
};
for(var i = 0, f = document.forms; i < f.length; ++i) {
f[i].focussedElement = null;
for(var j = 0, e = f[i].elements; j < e.length; ++j) {
e[j].onfocus = n;
e[j].onblur = p;
}
}
};
Notes on your original code:
Forms needn't always be collected as properties of the document element, and elements needn't always be collected as properties of their forms. You should use the document.forms and form.elements collections instead (for example, rather than document.create.title.value, you should use document.forms.create.elements.title.value). However, it's a much better and more flexible idea to pass the form or element to the function as a parameter, rather than hard-coding it into the function. That way, if you change the name of the form or its elements, the function still works.
Bookmarks