OBJECTIVE: given a form with a set of elements sharing the same
name prefix followed by a
progressive number, reset their values.
A simple task - a similar task is
Javascript Form Clear Default Input Values Upon Submit.
function resetValuesByName(form, namePrefix, start, newValue){
if(typeof namePrefix!='string'){namePrefix='';}; start=Math.abs(start)||0; newValue=newValue||'';
for(var i=start; form[namePrefix+i]; i++){form[namePrefix+i].value=newValue;}
return false;
}Remove colors
INPUTS: first the form
object such as, for instance, it would be retrieved via
document.getElementById
Then a string representing the shared prefix name of all the form fields whose values must be reset.
Then an optional number, defaulting to
zero, representing the starting number to append to the previous argument, and to increment by one unit at each iteration, in order to locate the form elements.
Then an optional string, defaulting to an empty string, which is the new value to assign.
RETURNS: boolean
false
CAVEATS: the function uses the associative nature of every form, that holds as its properties its elements by element names.
Of course, if the numerical progression of the suffix is broken (that is, a prefix name followed by 1,2,3 and then, say, suddenly jumping to prefix followed by 5 instead than by 4) the function would halt.
Example of use:
<script>
//include your function here, then:
</script>
<form id="foo">Fill in, then click the reset link:<br />
<input type="text" name="a_name_prefix1" /><br />
<input type="text" name="a_name_prefix2" /><br />
<input type="text" name="other_name" /><br />
<input type="text" name="a_name_prefix3" /><br />
</form><br />
<a href="#" onclick="return resetValuesByName( document.getElementById('foo'), 'a_name_prefix', 1 )">reset</a>
Remove colors