Path: csiph.com!news.mixmin.net!weretis.net!feeder1.news.weretis.net!news.solani.org!.POSTED!not-for-mail From: Thomas 'PointedEars' Lahn Newsgroups: comp.lang.javascript Subject: Re: Give alert if form checkbox array is empty Date: Thu, 21 Jan 2016 00:59:20 +0100 Organization: PointedEars Software (PES) Lines: 67 Message-ID: <1851504.v7CTRd67sv@PointedEars.de> References: Reply-To: Thomas 'PointedEars' Lahn Mime-Version: 1.0 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: 8Bit X-Trace: solani.org 1453334361 25194 eJwFwYEBwCAIA7CXEGiZ56CW/09YguDirSSYGEyOCoeGxDUNTeX7xIZNZ4Srvm7XYgvkez8VTBEE (20 Jan 2016 23:59:21 GMT) X-Complaints-To: abuse@news.solani.org NNTP-Posting-Date: Wed, 20 Jan 2016 23:59:21 +0000 (UTC) User-Agent: KNode/4.14.2 X-User-ID: eJwNwokRwDAIA7CVzGs6DoHL/iOkJ4Wl5NAz0uP+moulFHHsqKOgXYgyfuJatXW2u2c0HDd9BZClaQ/z6gMrOhSH Cancel-Lock: sha1:1VIDjVsYDdkv5EmHpfDv9CYYtzI= X-NNTP-Posting-Host: eJwFwYkRwDAIA7CVAsa4GafHs/8IkYi0LEUyg8vF0LAjCb8psCxaD318ip+j2m6pGgY/9zwUPxCc Xref: csiph.com comp.lang.javascript:29382 Bunyip wrote: ^^^^^^ Please post using your full (real) name. > On Wed, 20 Jan 2016 10:28:29 +0100, Stefan Weiss > wrote: >>var subjectTypes = document.getElementsByName('subjectType[]'); >>for (var i = 0; i < subjectTypes.length; i++) { >> if (subjectTypes[i].checked) { >> > Many thanks, Stefan. This worked: > > var subjectTypes = document.getElementsByName('subjectType[]'); The disadvantage of using “document.getElementsByName()” over (here) the “form.elements” collection is that the former applies to all names in the document, while the latter only applies to the controls of that specific form. As as result, the latter is not only more efficient, but also less error-prone. Also, since the reference to the form element object is available in the event-handler attribute and in the listener with “this” (unless you unwisely use attachEvent()), the latter can be made to work regardless of the form name or position in the document. > var STchecked; Names should not start with a capital letter unless the value of the variable/property is intended to be a reference to a constructor. > for (var i = 0; i < subjectTypes.length; i++) { for (var i = 0, len = subjectTypes.length; i < len; i++) { But make sure that you do not modify “len” elsewhere. > if (subjectTypes[i].checked) { > STchecked = 1; > break; > } You should familiarize yourself with the Array methods introduced with implementations of ECMAScript Edition 5 at the latest, in order to not write explicit loops where they are not necessary. Examples have been given. Keep in mind that the built-in implementation of an algorithm is naturally always the most efficient one to use as it is, at best, provided in machine code already. > } > if (!STchecked){ > alert("Please enter the subject type"); Again, do not use alert(); update the form instead. In this case, the “fieldset” element for the checkbox group could be highlighted. > form.focus(); Pointless. The form already receives the focus once the alert message is closed, since it had the focus when it was submitted. You should focus the offending form control instead. -- PointedEars FAQ: | SVN: Twitter: @PointedEars2 | ES Matrix: Please do not cc me. / Bitte keine Kopien per E-Mail.