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


Groups > comp.lang.javascript > #8194

Re: newbie Javascript checked is null or not an object

Message-ID <1675485.FNyZMGkNft@PointedEars.de> (permalink)
From Thomas 'PointedEars' Lahn <PointedEars@web.de>
Organization PointedEars Software (PES)
Date 2011-11-10 14:30 +0100
Subject Re: newbie Javascript checked is null or not an object
Newsgroups comp.lang.javascript
References <7a88bf5f-7821-49ca-85a8-bca655c28a36@u9g2000vbx.googlegroups.com> <4ebab172$0$14430$426a34cc@news.free.fr> <4ebb0ac7$0$28592$a8266bb1@newsreader.readnews.com>
Followup-To comp.lang.javascript

Followups directed to: comp.lang.javascript

Show all headers | View raw


Denis McMahon wrote:

> On Wed, 09 Nov 2011 17:59:28 +0100, Elegie wrote:
> 
> var ff=document.form1.elements[elemnum]; // untested
> 
> /* then wrap the tests so that it only looks for the checked property on
> actual checkboxes */
> 
> if (ff.type == "checkbox") ff.checked = !ff.checked; // untested

A more reliable test would be 

  if (typeof ff.checked != "undefined")
  {
    ff.checked = !ff.checked;
  }

That is, you should test the property that you intent to access.  But if 
this is for checkboxes only, your way should suffice.

> /* or it will barf when you test an element that doesn't have the checked
> property. */

That is not likely.  While host objects do have a reputation for being 
unreliable, many of them will allow the addition of new properties, and will 
yield the standard `undefined' value when accessing non-existent properties 
(and !undefined === true).  It is still unreliable, though.

However, this code should never be applied to objects that do not have a 
`checked' property in the first place, which renders the question moot and 
the test unnecessary.

> You could even (and I'm sure someone will shortly post saying why this is
> bad,

It is bad,

> but probably not actually explaining (a) why

because it is comparably inefficient, incompatible, and unnecessary.

> or (b) the 'proper' way of doing it)

The proper way of doing it is described in the FAQ of this newsgroup and 
involves the standard backwards-compatible `forms' and `elements' 
collections.

Put politely, you would be well-advised to read the FAQ and listen to the 
people who very obviously have a lot more experience in this field than you 
have.


PointedEars
-- 
Danny Goodman's books are out of date and teach practices that are
positively harmful for cross-browser scripting.
 -- Richard Cornford, cljs, <cife6q$253$1$8300dec7@news.demon.co.uk> (2004)

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


Thread

newbie Javascript checked is null or not an object shall@uaex.edu - 2011-11-09 08:10 -0800
  Re: newbie Javascript checked is null or not an object Elegie <elegie@anonymous.invalid> - 2011-11-09 17:59 +0100
    Re: newbie Javascript checked is null or not an object Denis McMahon <denismfmcmahon@gmail.com> - 2011-11-09 23:20 +0000
      Re: newbie Javascript checked is null or not an object Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2011-11-10 14:30 +0100
        Re: newbie Javascript checked is null or not an object Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2011-11-12 13:32 +0100
      Re: newbie Javascript checked is null or not an object shall@uaex.edu - 2011-11-10 05:30 -0800
      Re: newbie Javascript checked is null or not an object David Mark <dmark.cinsoft@gmail.com> - 2011-11-10 06:36 -0800

csiph-web