0

I use this code to reset my radio buttons, but it doesn't reset my text fields. How can I make it reset my text fields?

function rensa(obj)
{
    for(var i=0; i<obj.length; i++)    
         obj[i].checked = false;

}
ThinkingStiff
  • 64,767
  • 30
  • 146
  • 239
jenny
  • 11
  • 1
  • `obj[i].value = ''`, but than again how do you know if it's a text field. Furthermore, how to reset a `select` element, and so on and so forth... Do not try to re-invent the wheel. – Shef Aug 26 '11 at 12:11
  • Have you seen ? – aces. May 30 '12 at 17:28

3 Answers3

7

Why don't you just use reset(), e.g.:

document.myform.reset();
Prisoner
  • 27,391
  • 11
  • 73
  • 102
  • `reset()` might not be what the OP wants, since it will reset the values to the ones the page originally had instead of clearing them (eg: when a text input field has a non-empty `value` attribute or a checkbox has a `checked` attribute) – NullUserException Apr 11 '12 at 19:23
1

The Form element has a reset() method

amal
  • 1,369
  • 8
  • 15
-1
function rensa(obj)
{
    for(var i=0; i<obj.length; i++){    
         obj[i].checked = false;
         obj[i].value = "";
  }

}
sushil bharwani
  • 29,685
  • 30
  • 94
  • 128
  • 1
    -1 - what if the default value wasn't empty string, or radio buttons and checkboxes that should be checked by default? What about other elements like select? Or buttons where the value is used for the label? Just call the form's *reset* method (as suggested in other answers). – RobG Aug 26 '11 at 12:24
  • @RobG . Yes. There can be a lot of things. And i agree with your comments. But at times on SO. I have found that users asks just part of there problems and at that time they need the specific answers. I think the author agree with the answer. And the answer is not the complete form reset. – sushil bharwani Aug 26 '11 at 12:32