HighDots Forums  

Detect empty ID

Javascript JavaScript language (comp.lang.javascript)


Discuss Detect empty ID in the Javascript forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
weblinkunlimited@gmail.com
 
Posts: n/a

Default Detect empty ID - 03-15-2008 , 08:10 PM






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?

Thanks

Reply With Quote
  #2  
Old   
RobG
 
Posts: n/a

Default Re: Detect empty ID - 03-15-2008 , 09:26 PM






On Mar 16, 10:10*am, weblinkunlimi... (AT) gmail (DOT) com wrote:
Quote:
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') {
typeof is an operator, not a function - you don't need to wrap its
argument in brackets.


Quote:
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?
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 >

--
Rob


Reply With Quote
  #3  
Old   
Thomas 'PointedEars' Lahn
 
Posts: n/a

Default Re: Detect empty ID - 03-16-2008 , 12:39 PM



RobG wrote:
Quote:
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.)

[2] http://www.mozilla.org/js/language/E262-3.pdf

Quote:
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
Obviously the "less strict" variant is to be recommended here. One can
safely assume that a working D::gEBI() implementation would return a
false-value if the element specified by the passed ID value did not exist,
and a true-value if it did.


PointedEars
--
realism: HTML 4.01 Strict
evangelism: XHTML 1.0 Strict
madness: XHTML 1.1 as application/xhtml+xml
-- Bjoern Hoehrmann


Reply With Quote
  #4  
Old   
Dr J R Stockton
 
Posts: n/a

Default Re: Detect empty ID - 03-16-2008 , 04:13 PM



In comp.lang.javascript message <e86c6aba-c140-4fdd-8083-1b95f681ef2a@h1
1g2000prf.googlegroups.com>, Sat, 15 Mar 2008 18:36:48, RobG
<rgqld (AT) iinet (DOT) net.au> posted:
Quote:
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.

--
(c) John Stockton, nr London, UK. ?@merlyn.demon.co.uk Turnpike v6.05.
Web <URL:http://www.merlyn.demon.co.uk/> - w. FAQish topics, links, acronyms
PAS EXE etc : <URL:http://www.merlyn.demon.co.uk/programs/> - see 00index.htm
Dates - miscdate.htm moredate.htm js-dates.htm pas-time.htm critdate.htm etc.


Reply With Quote
  #5  
Old   
RobG
 
Posts: n/a

Default Re: Detect empty ID - 03-16-2008 , 10:12 PM



On Mar 17, 2:39 am, Thomas 'PointedEars' Lahn <PointedE... (AT) web (DOT) de>
wrote:
Quote:
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?


Quote:
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.


--
Rob


Reply With Quote
  #6  
Old   
RobG
 
Posts: n/a

Default Re: Detect empty ID - 03-16-2008 , 11:22 PM



On Mar 17, 6:13 am, Dr J R Stockton <j... (AT) merlyn (DOT) demon.co.uk> wrote:
Quote:
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.
That there are a number of different interpretations of the OP's post
indicates that perhaps the relevant data should be put into an
attribute value of the span rather than the text content. Maybe a
particular class value would suit, say:

<span class="val_yes ..." ...> ... </span>


to remove all doubt and allow the content to be whatever it needs to
be.


--
Rob


Reply With Quote
  #7  
Old   
Thomas 'PointedEars' Lahn
 
Posts: n/a

Default Re: Detect empty ID - 03-17-2008 , 02:44 PM



RobG wrote:
Quote:
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?
I'm afraid you are missing the point. Fallacy: Hasty generalization.

Quote:
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.
No, the ECMAScript Specification is very clear on what `Null' and `null' are.

Quote:
It says Null is a primitive,
Certainly it does not.

Quote:
yet also specifies that typeof Null returns "object" (§ 11.4.3). If
Undefined returns undefined, it seems to me that Null should return null.
You are missing the point: null != Null, and undefined != Undefined.

In fact, neither `Null' nor `Undefined' are exposed (as constructors) in
implementations. Therefore `typeof Null' as well as `typeof Undefined'
yield "undefined", unless you declare those identifiers.

However, it does make sense that `typeof undefined' yields "undefined", and
that `typeof null' yields "object", because `Null' is an object-related type
and `null' is an object-related value, while `Undefined' and `undefined' are
not object-related.


PointedEars
--
var bugRiddenCrashPronePieceOfJunk = (
navigator.userAgent.indexOf('MSIE 5') != -1
&& navigator.userAgent.indexOf('Mac') != -1
) // Plone, register_function.js:16


Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.