
Originally Posted by
abskure
I want to use javascript to make all other fields empty when you focus on one.
Have you considered the implications of that? What if the user presses the Tab key to move between fields, and focuses on that control on their way to some other control. Should these fields still be cleared? Moreover, will you be relying on this when the data is sent? If the user has scripting disabled, will uncleared fields confuse whatever's server-side?
You may need to rethink your design.
Is there an easy way of doing this rather than listing
Code:
onfocus="document.propSearch.City.value='';
document.propSearch.Subdivision.value= '';
document.propSearch.ListingID.value = '';"
There are a couple of ways to do this. For instance, one could pass the names of controls along with a reference to the form:
Code:
function clear(form) {
for (var i = 1; i < arguments.length; ++i)
form.elements[arguments[i]].value = '';
}
HTML Code:
<input ... onfocus="clear(this.form, 'City', 'Subdivision', 'ListingID');">

Originally Posted by
jscheuer1
Code:
function clearAll(el){
var texts=document.getElementsByTagName('input')
for (var i_tem = 0; i_tem < texts.length; i_tem++)
if (texts[i_tem].type=='text'&&texts[i_tem]!==el)
texts[i_tem].value=''
}
I think the call to gEBTN can be avoided. The OP should only want to clear controls within a particular form, so simply iterating through the elements collection of that form should suffice:
Code:
function clearAll(form, skip) {
var controls = form.elements;
for (var i = 0, n = controls.length; i < n; ++i) {
var current = controls[i];
if ((current.type == 'text') && (!skip || (current != skip)))
current.value = '';
}
}
A call such as:
HTML Code:
<input ... onfocus="clearAll(this.form);">
would clear all text controls in that form. A call such as:
HTML Code:
<input ... onfocus="clearAll(this.form, this);">
would clear all text controls except the one that just received focus.
Mike
Bookmarks