Take the 2-minute tour ×

I have a form that looks something like this

    <INPUT TYPE='hidden' NAME='search0' VALUE='a'> 
    <INPUT TYPE='hidden' NAME='search1' VALUE='b'>
    ... etc 
    <INPUT TYPE='hidden' NAME='search100' VALUE='abc'>

In my javascript i would like to do something along the lines of :

     document.forms[0].search+i+.value = '';

Clearly this is not valid syntax, I am just not sure how to go about getting all 100 of my hidden inputs and change all their values to empty. Any help greatly appreciated!

share|improve this question
thanks for formatting the FORM for me j08691, i didnt know how to properly format –  Stefan Sep 27 '12 at 20:28

2 Answers 2

up vote 3 down vote accepted

Note that .0 would not be valid syntax - therefore you use [0] because you can put any expression inside such brackets (e.g. numbers, strings). Indeed, you can also use the + operator inside since + can be applied to expressions. This solves your issue:

document.forms[0]["search" + i].value = '';

You could also use ["forms"] instead of .forms, etc. Basically, the . notation is only a subset of the [] notation.

share|improve this answer
thank you for fast reply, this was exactly what i was looking for. Will mark as correct in 5 minutes when the website allows me to –  Stefan Sep 27 '12 at 20:34

The jQuery "almost but not really" equivalent would be something like this:

  /* all input that has a name that starts with search */
  $("input[name^=search]").each(function (){
    /* set the value to empty */
share|improve this answer
It would not be completely functionally equal, but anyway you don't need .each. –  pimvdb Sep 27 '12 at 20:35
@pimvdb That is true. –  ejohansson Sep 27 '12 at 20:36

Your Answer


