I am write code in JS. When I hit the Submit Button, the JS code needs to check every hidden field. If the hidden field has a value, it needs to be set back to Null

I am writing in Javascript.  In my validation code, upon hitting "Submit", I need the code to check every "Hidden" field in the form.  If the hidden field has a value, it must be changed back to "Null"
LGooginAsked:
Who is Participating?
 
silemoneConnect With a Mentor Commented:
<script>
var theForm = document.forms[0];
var elem = theForm.elements;
for(var i=0;i<elem.length;i++){
  if(elem[i].type=="hidden" && elem[i].value !=""){      
      elem.value = "";
      }
    }
  }
}
</script>
0
 
silemoneConnect With a Mentor Commented:
this scripts locates all hidden fields and change their value to empty string "" if they are not already ""...

<script>
var theForm = document.forms[0];
var elem = theForm.elements;
for(var i=0;i<elem.length;i++){
  if(elem[i].type=="hidden" && elem[i].value !=""){      

      //do whatever else with the value you do here...store it in a variable, array,  etc..
      elem.value = "";
      }
    }
  }
}
</script>
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.