3 Replies - 599 Views - Last Post: 08 March 2012 - 08:27 AM

#1 black_yurizan  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 89
  • Joined: 18-June 11

How to clear values in forms

Posted 08 March 2012 - 07:54 AM

Hello I've made a set of input values from a from that will generate a type of characteristic of a person. For instance, one input will generate the race, or sex, or body type. I successfully made the program work, the problem is that when I refresh the page values will still be in the input text box. So I'm trying to figure a way to clear the values whenever I load or refresh the page.This is what I have done thus far


<script type="text/javascript">
document.write("random character generator".fontsize(12)+"</br>");

window.addEventListener("load", clear, false);
window.onload=  clear();
function clear() { document.form.in.value="";}
</script>
<form name="form">
race:<input type="text" name="in" id="race"/>
sex:<input type="text" name="in" id="sex"/>
body:<input type="text" name="in" id="body"/>
</form>





The addEventListener should have cleared all the text boxes, but for some reason it's not. Does anyone know what I should do?

Wait, I figured it out, "in" is a keyword so javascript got confused. So.. what is "in" used for?

Is This A Good Question/Topic? 0
  • +

Replies To: How to clear values in forms

#2 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3577
  • View blog
  • Posts: 10,442
  • Joined: 08-June 10

Re: How to clear values in forms

Posted 08 March 2012 - 07:55 AM

the "some reason" is that document.form.in does not have a value attribute, because it is a HTMLCollection (a set of elements). just call HTMLFormElement.reset() (the reset() method of the form).
Was This Post Helpful? 0
  • +
  • -

#3 black_yurizan  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 89
  • Joined: 18-June 11

Re: How to clear values in forms

Posted 08 March 2012 - 08:24 AM

So am I suppose to write?
<script>
window.onload= HTMLFormElement.reset();
</script>




Was This Post Helpful? 0
  • +
  • -

#4 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3577
  • View blog
  • Posts: 10,442
  • Joined: 08-June 10

Re: How to clear values in forms

Posted 08 March 2012 - 08:27 AM

no. HTMLFormElement is only the DOM interface for form elements. obviously you would have to use your actual form.

PS. if you write it that way, it won’t work (maybe except IE). event handlers expect functions (function references) to be given.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1