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


Groups > comp.lang.javascript > #29376

Re: Give alert if form checkbox array is empty

From Thomas 'PointedEars' Lahn <PointedEars@web.de>
Newsgroups comp.lang.javascript
Subject Re: Give alert if form checkbox array is empty
Date 2016-01-20 17:15 +0100
Organization PointedEars Software (PES)
Message-ID <3964788.uURLdzOTfL@PointedEars.de> (permalink)
References <d5vt9b95kifj8nb7phn75pl16tpijop14s@4ax.com> <f1e60$569f43fc$11450e97$16338@nntpswitch.blueworldhosting.com> <5888602.Nj0jmESFCg@PointedEars.de> <405a9$569fab1d$11450e97$16733@nntpswitch.blueworldhosting.com>

Show all headers | View raw


Cezary Tomczyk wrote:

> On 20/01/2016 14:52, Thomas 'PointedEars' Lahn wrote:
> 
> I am not sure whether using implicitly labels is a good idea. I think
> there were some problems with screen readers, but I am not quite sure at
> the moment.

I would like to see evidence of that.  I would understand problems with 
layout engines (like dotted borders around the checkbox on focus), but 
screen readers?
 
> Also, on the other page W3C says:
> 
> "Whenever possible, use the label element to explicitly associate text
> with form elements."
> 
> Source: https://www.w3.org/WAI/tutorials/forms/labels/

But also:

<https://www.w3.org/WAI/tutorials/forms/labels/#associating-labels-implicitly>

| In some situations, form controls cannot be labelled explicitly. For 
| example, a content author might not know the id of a form field generated 
| by a script, or that script might not add an id at all. In this case the 
| label element is used as a container for both the form control and the 
| label text, so that the two are associated implicitly.

>>>> […]
>>>> <input type="submit" value="Add artwork" name="add_artwork">
>>>> </form>
>>>>
>>>> I need to check that at least one sunbjectType[] has been checked and,
>>>> if not, throw an alert.
>>> [...]
>>> I would use document.querySelector method. Example:
>> I would not; it is inefficient incompatible overkill here.
> 
> I would, personally, rather measure if this impacts on performance
> significantly and decide to change the approach or not. Otherwise
> "inefficient" here, I think, is not important.

document.querySelector(), if it works, is as efficient as the layout engine 
when matching the selector in a stylesheet against the document.
 
> As for compatibility - in some old environments that may not be
> supported, but again, if somebody have to support those environments
> then different solution can be considered.

The problem with document.querySelector() is that it depends not only on the 
layout engine’s DOM support but also on the degree of its CSS support.  You 
need a fallback in case you get a false negative from lack of support (for 
example, support for “:checked” is not a given if document.querySelector() 
is supported), but then you should have implemented the fallback approach 
that does not use document.querySelector() in the first place.
 
>>> function processSubmit(event) {
>>>     var isOneInputChecked = document.querySelector('#testForm
                                                        ^^^^^^^^^
>>> input[name="subjectType[]"]:checked');
>>
>> Here you are looking for the form element object that you have just
>> found.
> 
> My mistake. The var declaration I left accidentally.

That is another point, but it was not mine.  See the marking.
 
>>>     if (isOneInputChecked) {
>>>       window.alert('You have checked at least one input');
>>>     }
>>>
>>>     event.preventDefault();
>> There is no reason to *always* prevent the default submit action, and if
>> you use the event-handler attribute as the OP did, you can simply return
>> “false” instead (which is backwards-compatible).
> 
> In the event-handler attribute return "false" is fine, but I think, when
> used in my example it also stops propagation and that may not be always
> desired.

The point here is that your call of event.preventDefault() is independent of 
“isOneInputChecked”, in contrast to how the OP’s code was supposed to work.
 
>> window.alert() should not be used here.  […]
> 
> The window.alert is used there just for the sake of example. ;-)

Hopefully.  It was in the OP’s code already.
 
>>> formElement.addEventListener('submit', processSubmit);
>>
>> It is not necessary to do this (which does not work in in IE < 9 and IE 9
>> in Compatibility Mode) if you specify the “onsubmit” attribute in the
>> markup as the OP did.
> 
> Alternatively some library might be used to handle cross-browser issues
> with events.

There are libraries that emulate addEventListener() the wrong way.  Granted, 
I have just removed active support for IE < 9 this week from our code.
 
> Also, I just prefer not to use inline event-handlers in favour of
> addEventListener method.

Why?
 
-- 
PointedEars
FAQ: <http://PointedEars.de/faq> | SVN: <http://PointedEars.de/wsvn/>
Twitter: @PointedEars2 | ES Matrix: <http://PointedEars.de/es-matrix>
Please do not cc me. / Bitte keine Kopien per E-Mail.

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


Thread

Give alert if form checkbox array is empty Bunyip <bunyip@koalanet.com.au> - 2016-01-20 13:28 +1000
  Re: Give alert if form checkbox array is empty JJ <jj4public@vfemail.net> - 2016-01-20 12:30 +0700
    Re: Give alert if form checkbox array is empty Bunyip <bunyip@koalanet.com.au> - 2016-01-20 17:55 +1000
      Re: Give alert if form checkbox array is empty JJ <jj4public@vfemail.net> - 2016-01-20 19:29 +0700
        Re: Give alert if form checkbox array is empty "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2016-01-20 13:46 +0100
  Re: Give alert if form checkbox array is empty Cezary Tomczyk <cezary.tomczyk@gmail.com> - 2016-01-20 08:23 +0000
    Re: Give alert if form checkbox array is empty Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2016-01-20 15:52 +0100
      Re: Give alert if form checkbox array is empty Cezary Tomczyk <cezary.tomczyk@gmail.com> - 2016-01-20 15:43 +0000
        Re: Give alert if form checkbox array is empty Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2016-01-20 17:15 +0100
          Re: Give alert if form checkbox array is empty Jon Ribbens <jon+usenet@unequivocal.co.uk> - 2016-01-20 17:53 +0000
            Re: Give alert if form checkbox array is empty Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2016-01-20 20:39 +0100
              Re: Give alert if form checkbox array is empty Jon Ribbens <jon+usenet@unequivocal.co.uk> - 2016-01-20 22:46 +0000
          Re: Give alert if form checkbox array is empty Cezary Tomczyk <cezary.tomczyk@gmail.com> - 2016-01-21 07:53 +0000
            Re: Give alert if form checkbox array is empty Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2016-01-21 10:52 +0100
              Re: Give alert if form checkbox array is empty Cezary Tomczyk <cezary.tomczyk@gmail.com> - 2016-01-21 10:26 +0000
                Re: Give alert if form checkbox array is empty Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2016-01-22 00:07 +0100
  Re: Give alert if form checkbox array is empty Stefan Weiss <krewecherl@gmail.com> - 2016-01-20 10:28 +0100
    Re: Give alert if form checkbox array is empty Bunyip <bunyip@koalanet.com.au> - 2016-01-21 08:05 +1000
      Re: Give alert if form checkbox array is empty Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2016-01-21 00:59 +0100
        Re: Give alert if form checkbox array is empty Stefan Weiss <krewecherl@gmail.com> - 2016-01-21 03:17 +0100
          Re: Give alert if form checkbox array is empty Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2016-01-21 08:54 +0100
            Re: Give alert if form checkbox array is empty Stefan Weiss <krewecherl@gmail.com> - 2016-01-21 12:17 +0100
        Re: Give alert if form checkbox array is empty John Harris <niam@jghnorth.org.uk.invalid> - 2016-01-21 14:16 +0000
        Re: Give alert if form checkbox array is empty Scott Sauyet <scott.sauyet@gmail.com> - 2016-01-21 19:52 -0800
          Re: Give alert if form checkbox array is empty Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2016-01-22 09:48 +0100
            Re: Give alert if form checkbox array is empty Scott Sauyet <scott.sauyet@gmail.com> - 2016-01-22 10:45 -0800
              Re: Give alert if form checkbox array is empty Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2016-01-23 19:41 +0100
                Re: Give alert if form checkbox array is empty Scott Sauyet <scott.sauyet@gmail.com> - 2016-01-24 17:51 -0800
                Re: Give alert if form checkbox array is empty Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2016-01-25 19:46 +0100
                Re: Give alert if form checkbox array is empty Scott Sauyet <scott.sauyet@gmail.com> - 2016-01-27 11:09 -0800
        Re: Give alert if form checkbox array is empty Dr J R Stockton <reply1600@merlyn.demon.co.uk.invalid> - 2016-01-23 22:22 +0000
        Re: Give alert if form checkbox array is empty Scott Sauyet <scott.sauyet@gmail.com> - 2016-01-27 11:06 -0800
          Re: Give alert if form checkbox array is empty Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2016-01-28 14:46 +0100
            Re: Give alert if form checkbox array is empty Gene Wirchenko <genew@telus.net> - 2016-01-28 10:30 -0800
              Re: Give alert if form checkbox array is empty Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2016-01-28 20:06 +0100
                Re: Give alert if form checkbox array is empty Scott Sauyet <scott.sauyet@gmail.com> - 2016-01-28 23:32 -0800
                Re: Give alert if form checkbox array is empty "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2016-01-29 09:05 +0100
                Re: Give alert if form checkbox array is empty Scott Sauyet <scott.sauyet@gmail.com> - 2016-01-29 06:02 -0800
                Re: Give alert if form checkbox array is empty Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2016-01-29 20:54 +0100
                Re: Give alert if form checkbox array is empty John Harris <niam@jghnorth.org.uk.invalid> - 2016-01-29 10:36 +0000
                Re: Give alert if form checkbox array is empty Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2016-02-09 04:39 +0100
                Re: Give alert if form checkbox array is empty John Harris <niam@jghnorth.org.uk.invalid> - 2016-02-09 18:07 +0000
                Re: Give alert if form checkbox array is empty Gene Wirchenko <genew@telus.net> - 2016-01-29 09:32 -0800
                Re: Give alert if form checkbox array is empty Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2016-01-29 20:34 +0100
                Re: Give alert if form checkbox array is empty Aleksandro <aleksandro@gmx.com> - 2016-01-29 16:50 -0300
                Re: Give alert if form checkbox array is empty Stefan Weiss <krewecherl@gmail.com> - 2016-01-30 03:31 +0100
                Re: Give alert if form checkbox array is empty Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2016-01-30 04:19 +0100
                Re: Give alert if form checkbox array is empty Stefan Weiss <krewecherl@gmail.com> - 2016-01-30 05:01 +0100
              Re: Give alert if form checkbox array is empty Aleksandro <aleksandro@gmx.com> - 2016-01-28 16:39 -0300
            Re: Give alert if form checkbox array is empty Scott Sauyet <scott.sauyet@gmail.com> - 2016-01-29 06:04 -0800
              Re: Give alert if form checkbox array is empty Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2016-01-29 20:54 +0100
                Re: Give alert if form checkbox array is empty Scott Sauyet <scott.sauyet@gmail.com> - 2016-01-29 14:55 -0800
                Re: Give alert if form checkbox array is empty Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2016-01-30 04:55 +0100
                Re: Give alert if form checkbox array is empty Scott Sauyet <scott.sauyet@gmail.com> - 2016-01-30 15:48 -0800
                Re: Give alert if form checkbox array is empty Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2016-01-31 21:02 +0100
                Re: Give alert if form checkbox array is empty Scott Sauyet <scott.sauyet@gmail.com> - 2016-01-31 13:08 -0800
                Re: Give alert if form checkbox array is empty Andrew Poulos <ap_prog@hotmail.com> - 2016-02-01 12:28 +1100
                Re: Give alert if form checkbox array is empty Scott Sauyet <scott.sauyet@gmail.com> - 2016-02-01 17:58 -0800
  Re: Give alert if form checkbox array is empty "Michael Haufe (TNO)" <tno@thenewobjective.com> - 2016-01-20 06:25 -0800
  Re: Give alert if form checkbox array is empty Aleksandro <aleksandro@gmx.com> - 2016-01-20 12:54 -0300

csiph-web