Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!gegeweb.42!gegeweb.eu!nntpfeed.proxad.net!proxad.net!feeder2-2.proxad.net!newsfeed.arcor.de!newsspool2.arcor-online.net!news.arcor.de.POSTED!not-for-mail Content-Type: text/plain; charset="ISO-8859-1" Message-ID: <1675485.FNyZMGkNft@PointedEars.de> From: Thomas 'PointedEars' Lahn Reply-To: Thomas 'PointedEars' Lahn Organization: PointedEars Software (PES) Date: Thu, 10 Nov 2011 14:30:24 +0100 User-Agent: KNode/4.4.11 Content-Transfer-Encoding: 7Bit 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 MIME-Version: 1.0 Lines: 58 NNTP-Posting-Date: 10 Nov 2011 14:30:24 CET NNTP-Posting-Host: 5dbcd9ce.newsspool1.arcor-online.net X-Trace: DXC=PMca]L?`0F1_A0jCfgHO6>ic==]BZ:af>4Fo<]lROoR1<`=YMgDjhg2l<2;WLh_V17DZm8W4\YJN 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, (2004)