![]() | |
#1
| |||
| |||
|
#2
| |||
| |||
|
|
Hello, For an automatic script I want to detect if an <span id='whatever'>yes or no</span> is true or false. I thought by doing it by this way: if(typeof(document.getElementById('shoe1'))!='unde fined') { |
|
do this }else{ do that } But on all document.getElementById('whatever') I get an 'object' as result. By which way can I detect if it is TRUE or FALSE? |
#3
| |||
| |||
|
|
On Mar 16, 10:10 am, weblinkunlimi... (AT) gmail (DOT) com wrote: if(typeof(document.getElementById('shoe1'))!='unde fined') { [...] But on all document.getElementById('whatever') I get an 'object' as result. By which way can I detect if it is TRUE or FALSE? Your terminology seems a bit confused - you seem to want to determine if an element with id 'whatever' exists, however you are comparing the result using an inappropriate test. document.getElementById returns "null" (i.e. the Null object) if an element with the supplied ID doesn't exist[1]. |
|
The typeof operator will therefore return 'object', not 'undefined', so: if (document.getElementById('shoe1') === null ) { // do this or less strictly you could write: if (document.getElementById('shoe1')) { // do this 1. <URL: http://www.w3.org/TR/DOM-Level-2-Cor...ml#ID-getElBId |
#4
| |||
| |||
|
|
A regular expression would likely suit better here too, but surely only one test it needed? Test for either yes or no - if /\byes\b/ returns true, it's yes, otherwise it's no (and vice versa). |
#5
| |||
| |||
|
|
RobG wrote: On Mar 16, 10:10 am, weblinkunlimi... (AT) gmail (DOT) com wrote: if(typeof(document.getElementById('shoe1'))!='unde fined') { [...] But on all document.getElementById('whatever') I get an 'object' as result. By which way can I detect if it is TRUE or FALSE? Your terminology seems a bit confused - you seem to want to determine if an element with id 'whatever' exists, however you are comparing the result using an inappropriate test. document.getElementById returns "null" (i.e. the Null object) if an element with the supplied ID doesn't exist[1]. That assertion is not backed up by the Specification[1]. There is nothing in the Specification's ECMAScript Binding section that says the Specification's `null' value is to be identified with ECMAScript's `null' value. |
|
As for confused terminology, `null' is _not_ "the Null object", it is the sole value of the Null type, "a primitive value that represents the null, empty, or non-existent reference" (ECMAScript Ed. 3 Final[2], section 4.3.11.) |
#6
| |||
| |||
|
|
In comp.lang.javascript message <e86c6aba-c140-4fdd-8083-1b95f681ef2a@h1 1g2000prf.googlegroups.com>, Sat, 15 Mar 2008 18:36:48, RobG rg... (AT) iinet (DOT) net.au> posted: A regular expression would likely suit better here too, but surely only one test it needed? Test for either yes or no - if /\byes\b/ returns true, it's yes, otherwise it's no (and vice versa). And what if the reply is "yes and no" or "no, certainly not yes"? My reading of the OP is that the span should contain only either yes or no. Therefore (if feeling liberal, trim leading/training white-space and convert to lower case here) just test for 'yes', then test for 'no', and act accordingly. |
#7
| ||||
| ||||
|
|
On Mar 17, 2:39 am, Thomas 'PointedEars' Lahn <PointedE... (AT) web (DOT) de> wrote: RobG wrote: On Mar 16, 10:10 am, weblinkunlimi... (AT) gmail (DOT) com wrote: if(typeof(document.getElementById('shoe1'))!='unde fined') { [...] But on all document.getElementById('whatever') I get an 'object' as result. By which way can I detect if it is TRUE or FALSE? Your terminology seems a bit confused - you seem to want to determine if an element with id 'whatever' exists, however you are comparing the result using an inappropriate test. document.getElementById returns "null" (i.e. the Null object) if an element with the supplied ID doesn't exist[1]. That assertion is not backed up by the Specification[1]. There is nothing in the Specification's ECMAScript Binding section that says the Specification's `null' value is to be identified with ECMAScript's `null' value. It occurs in reality in the limited number of browsers I have available, do you know of a specific user agent where it doesn't? |
|
As for confused terminology, `null' is _not_ "the Null object", it is the sole value of the Null type, "a primitive value that represents the null, empty, or non-existent reference" (ECMAScript Ed. 3 Final[2], section 4.3.11.) The specification seems confused about that. |
|
It says Null is a primitive, |
|
yet also specifies that typeof Null returns "object" (§ 11.4.3). If Undefined returns undefined, it seems to me that Null should return null. |
![]() |
| Thread Tools | |
| Display Modes | |
| |