Path: csiph.com!usenet.pasdenom.info!dedibox.gegeweb.org!gegeweb.eu!nntpfeed.proxad.net!proxad.net!feeder2-2.proxad.net!newsfeed.arcor.de!newsspool1.arcor-online.net!news.arcor.de.POSTED!not-for-mail Content-Type: text/plain; charset="UTF-8" Message-ID: <2529079.e95EOKG17B@PointedEars.de> From: Thomas 'PointedEars' Lahn Reply-To: Thomas 'PointedEars' Lahn Organization: PointedEars Software (PES) Date: Wed, 26 Jun 2013 02:34:30 +0200 User-Agent: KNode/4.10.4 Content-Transfer-Encoding: 8Bit X-Face: %i>XG-yXR'\"2P/C_aO%~;2o~?g0pPKmbOw^=NT`tprDEf++D.m7"}HW6.#=U:?2GGctkL,f89@H46O$ASoW&?s}.k+&. MIME-Version: 1.0 Lines: 108 NNTP-Posting-Date: 26 Jun 2013 02:34:30 CEST NNTP-Posting-Host: 5d2ab748.newsspool4.arcor-online.net X-Trace: DXC=\\CHkUo0IGU@>[RYkFXOIP4IUK]gf^Kc3``coVM8R X-Complaints-To: usenet-abuse@arcor.de Xref: csiph.com comp.lang.javascript:20447 bugbuster wrote: > I have a javascipt function which I use to erase all the field in a >
. On those forms where yhere is a
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: > 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.