[Javascript] getting input elements

David T. Lovering dlovering at gazos.com
Wed Jul 16 15:55:58 CDT 2003


Hmmm... let me take a stab at this...

First off, most "input elements" don't work outside of a form, as the code behind
the form is intended to be the premier method of encoding the data conveyed by them
to some means of processing them.  Mind you, I don't use forms for this purpose (too
insecure for the manic, anal-retentive crowd I hang with), but I get along with those
that do... and even I still use forms as "wrappers" for data elements (like inputs),
simply to avoid all the nasty IE errors when the code rolls through my browser when I
don't.

The answer then, is use a neutered form as a wrapper...

<body>
<form name='myForm' action='javascript:void(null)'>
  <input type='text' name='myVal1' size=30>
  <input type='checkbox' name='myCheckbox1' value='on' checked>
  <input type='checkbox' name='myCheckbox2' value='off'>
  ...
</form>
</body>

The second part of your question "How can I get ... only the checkbox inputs" is solved
by the use of the getElementsByTagName and the ".type" declarator:

  var myInputArray = document.forms[0].getElementsByTagName("INPUT");
  for (var i=0; i<myInputs.length; i++) {
    if (myInputArray[i].type == 'checkbox') {
      // we have a winner!
      // do something neato with myInputArray[i]
    }
  }

Hope that helps.

-- Dave Lovering

Alan Roberto Romaniuc wrote:
> 
> Hi,
> 
>        I have a page with a lot of input elements, but without a form.
> How can I get, p.g., only the checkbox inputs to build a request url?
> 
> Tanhks
> 
> _______________________________________________
> Javascript mailing list
> Javascript at LaTech.edu
> https://lists.LaTech.edu/mailman/listinfo/javascript


More information about the Javascript mailing list