Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.javascript > #20447

Re: How can javascript detect a fieldset in an array of elements

Message-ID <2529079.e95EOKG17B@PointedEars.de> (permalink)
From Thomas 'PointedEars' Lahn <PointedEars@web.de>
Organization PointedEars Software (PES)
Date 2013-06-26 02:34 +0200
Subject Re: How can javascript detect a fieldset in an array of elements
Newsgroups comp.lang.javascript
References <379ks8thdutnrglv34mc63mr95kgbglopo@4ax.com>

Show all headers | View raw


bugbuster wrote:

> I have a javascipt function which I use to erase all the field in a
> <FORM>. On those forms where yhere is a <fieldset> tag the script
> encounters a problem.
> 
> my laptop is a win 7 laptop and I use the IE browser.

IE 9 or 10?
 
> Here is my javascript function

There is no javascript: <http://PointedEars.de/es-matrix>

> function clearForm(oForm)
> {
> var elements = oForm.elements;
> var count = elements.length;
> 
> //	oForm.reset();
> 
> for ( i = 0 ; i < elements.length ; i++ )

You should declare those identifiers variables, and avoid repeated property 
access.  It is safer, and a lot faster:

  for (var i = 0, len = elements.length; i < len; ++i)

> {
> 
> field_type = elements[i].type.toLowerCase();

    var field_type = elements[i].type.toLowerCase();

> switch(field_type)
> {
> case "text":
> case "password":
> case "textarea":
> case "hidden":
> elements[i].value = "";
> break;
> 
> case "radio":
> case "checkbox":
> if ( elements[i].checked ) {
> elements[i].checked = false;
> }
> break;
> 
> case "select-one":
> case "select-multiple":
> elements[i].selectedIndex = -1;
> break;
> 
> default:
> break;
> } // SWITCH
>     } // FOR
> }
> 
> When i use this function on a FORM that has a fieldset then the line
> 
> field_type = elements[i].type.toLowerCase();
> 
> 
> errors out. IE reports that "type" is undefined/has no value.
> 
> How can I modify the function to overcome this issue ?

The reason of the error is most likely not that there is a “fieldset” 
element, but that there are several controls that have the same name.  For 
example, it is common to put a radiobutton group into a “fieldset” element.  
All radiobuttons of a group must have the same name to be dependent on one 
another.

In that case,

  elements[i]

refers to a NodeList of references to those controls.  An object 
implementing the NodeList interface does not have a “type” property.
As the property does not exist, accessing it results in “undefined”,
and “undefined” has no properties.

You can use the fact above to your advantage to find out whether there is a 
group of same-named controls that need to be processed differently:

  var control = elements[i];
  if (typeof control.type == "undefined")
  {
    /* iterate over NodeList */
    for (var j = 0, len2 = control.length; j < len2; ++j)
    {
      // …
    }

    // …
  }


Please post using your real name next time.

-- 
PointedEars

Twitter: @PointedEars2
Please do not Cc: me. / Bitte keine Kopien per E-Mail.

Back to comp.lang.javascript | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

How can javascript detect a fieldset in an array of elements bugbuster <barryk_56@comcast.net> - 2013-06-25 19:22 -0400
  Re: How can javascript detect a fieldset in an array of elements Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2013-06-26 02:34 +0200
    Re: How can javascript detect a fieldset in an array of elements Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2013-06-26 02:39 +0200
    Re: How can javascript detect a fieldset in an array of elements John Harris <niam@jghnorth.org.uk.invalid> - 2013-06-26 16:10 +0100
      Re: How can javascript detect a fieldset in an array of elements Denis McMahon <denismfmcmahon@gmail.com> - 2013-06-26 21:21 +0000
        Re: How can javascript detect a fieldset in an array of elements Andrew Poulos <ap_prog@hotmail.com> - 2013-06-27 08:12 +1000
          Re: How can javascript detect a fieldset in an array of elements Tim Streater <timstreater@greenbee.net> - 2013-06-26 23:21 +0100
            Re: How can javascript detect a fieldset in an array of elements Andrew Poulos <ap_prog@hotmail.com> - 2013-06-27 11:58 +1000
        Re: How can javascript detect a fieldset in an array of elements Eric Bednarz <bednarz@fahr-zur-hoelle.org> - 2013-06-27 00:14 +0200
          Re: How can javascript detect a fieldset in an array of elements Christoph Michael Becker <cmbecker69@arcor.de> - 2013-06-27 00:45 +0200
            “javascript”? (was: How can javascript detect a fieldset in an array of elements) Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2013-06-27 13:36 +0200
              Re: “javascript”? Eric Bednarz <bednarz@fahr-zur-hoelle.org> - 2013-07-29 01:21 +0200
                Re: “javascript”? Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2013-07-30 08:51 +0200
            Re: How can javascript detect a fieldset in an array of elements Eric Bednarz <bednarz@fahr-zur-hoelle.org> - 2013-06-27 14:52 +0200
          Re: How can javascript detect a fieldset in an array of elements "Jukka K. Korpela" <jkorpela@cs.tut.fi> - 2013-06-27 14:04 +0300
            Re: How can javascript detect a fieldset in an array of elements Eric Bednarz <bednarz@fahr-zur-hoelle.org> - 2013-06-27 14:45 +0200
              Re: How can javascript detect a fieldset in an array of elements Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2013-06-27 15:11 +0200
        Re: How can javascript detect a fieldset in an array of elements Christoph Michael Becker <cmbecker69@arcor.de> - 2013-06-27 00:45 +0200
          “javascript”? (was: How can javascript detect a fieldset in an array of elements) Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2013-06-27 14:21 +0200
          Re: How can javascript detect a fieldset in an array of elements John Harris <niam@jghnorth.org.uk.invalid> - 2013-06-27 14:31 +0100
            Parlance (was: How can javascript detect a fieldset in an array of elements) Christoph Michael Becker <cmbecker69@arcor.de> - 2013-06-27 16:21 +0200
              Re: Parlance Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2013-06-27 20:46 +0200
                Re: Parlance John Harris <niam@jghnorth.org.uk.invalid> - 2013-06-28 14:33 +0100
                Re: Parlance Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2013-06-28 15:54 +0200
                Re: Parlance "Jukka K. Korpela" <jkorpela@cs.tut.fi> - 2013-06-28 17:08 +0300
                Re: Parlance Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2013-06-28 17:05 +0200
                Re: Parlance "Jukka K. Korpela" <jkorpela@cs.tut.fi> - 2013-06-28 18:16 +0300
                Re: Parlance Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2013-06-28 17:56 +0200
                Re: Parlance Tim Streater <timstreater@greenbee.net> - 2013-06-28 17:54 +0100
                Re: Parlance Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2013-06-28 20:50 +0200
                Re: Parlance Andrew Poulos <ap_prog@hotmail.com> - 2013-06-29 07:18 +1000
                Re: Parlance John Harris <niam@jghnorth.org.uk.invalid> - 2013-06-29 17:17 +0100
              Re: Parlance (was: How can javascript detect a fieldset in an array of elements) John Harris <niam@jghnorth.org.uk.invalid> - 2013-06-28 14:53 +0100
                Re: Parlance Christoph Michael Becker <cmbecker69@arcor.de> - 2013-07-15 00:42 +0200
                Re: Parlance John Harris <niam@jghnorth.org.uk.invalid> - 2013-07-15 16:01 +0100
  Re: How can javascript detect a fieldset in an array of elements "Jukka K. Korpela" <jkorpela@cs.tut.fi> - 2013-06-26 08:55 +0300
  Re: How can javascript detect a fieldset in an array of elements Dr J R Stockton <reply1300@merlyn.demon.co.uk.invalid> - 2013-06-27 21:27 +0100

csiph-web