You are here: irt.org | FAQ | JavaScript | Form | 5 | Q909 [ previous next ]
Yes, try the following which utilises the form elements type property for JavaScript 1.1 and greater, or the form elements name property for JavaScript 1.0:
<script language="JavaScript"><!-- function testDefaultValues(what) { var result = true; var output = ''; for (var i=0, j=what.elements.length; i<j; i++) { myName = what.elements[i].name; if (myName.indexOf('checkbox') > -1 || myName.indexOf('radio') > -1) { if (what.elements[i].checked && what.elements[i].defaultChecked) { output += what.elements[i].name + ' is still checked' + '\n'; result = false } } if (myName.indexOf('hidden') > -1 || myName.indexOf('password') > -1 || myName.indexOf('text') > -1) { if (what.elements[i].value == what.elements[i].defaultValue) { output += what.elements[i].name + ' still equals "' + what.elements[i].defaultValue + '"' + '\n'; result = false } } if (myName.indexOf('select') > -1) { for (var k=0, l=what.elements[i].options.length; k<l; k++) { if (what.elements[i].options[k].selected && what.elements[i].options[k].defaultSelected) { output += what.elements[i].name + ' option ' + k + ' is still selected' + '\n'; result = false } } } } if (output != '') alert(output); return result; } //--></script> <script language="JavaScript1.1"><!-- function testDefaultValues(what) { var result = true; var output = ''; for (var i=0, j=what.elements.length; i<j; i++) { myType = what.elements[i].type; if (myType == 'checkbox' || myType == 'radio') { if (what.elements[i].checked && what.elements[i].defaultChecked) { output += what.elements[i].name + ' is still checked' + '\n'; result = false } } if (myType == 'hidden' || myType == 'password' || myType == 'text' || myType == 'textarea') { if (what.elements[i].value == what.elements[i].defaultValue) { output += what.elements[i].name + ' still equals "' + what.elements[i].defaultValue + '"' + '\n'; result = false } } if (myType == 'select-one' || myType == 'select-multiple') { for (var k=0, l=what.elements[i].options.length; k<l; k++) { if (what.elements[i].options[k].selected && what.elements[i].options[k].defaultSelected) { output += what.elements[i].name + ' option ' + k + ' is still selected' + '\n'; result = false } } } } if (output != '') alert(output); return result; } //--></script> <form name="myForm" onSubmit="return testDefaultValues(document.myForm)"> <br><input name="checkbox1" type="checkbox"><input name="checkbox2" type="checkbox" checked> <br><input name="hidden" type="hidden" value="hidden value"> <br><input name="password" type="password" value="password value"> <br><input name="radio" type="radio" checked><input name="radio" type="radio"> <br><input name="text" type="text" value="text value"> <br><textarea name="textarea">textarea value</textarea> <br><select name="select1"> <option>0 <option>1 <option selected>2 <option>3 </select> <br><select name="select2" multipe size="2"> <option>0 <option>1 <option selected>2 <option>3 </select> <p><input type="submit" value="Test Default Values"> </form>