Detect empty ID

W

weblinkunlimited

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'))!='undefined') {
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
 
S

SAM

(e-mail address removed) a écrit :
Hello,

For an automatic script I want to detect if an <span id='whatever'>yes
or no</span> is true or false.


<html>
<script type="text/javascript">
function trueFalse(who) {
if( document.getElementById(who) ) { // does the span exist ?
var value = document.getElementById(who).innerHTML;
// does the span contain 'yes' or 'no' ?
if(value.indexOf('yes')>=0 || value.indexOf('no')>=0)
// if it does return 'yes is the value ?'
return value.indexOf('yes')>=0; // that breaks here if ok
}
return 'error';
}
</script>
<p><span id="yn_1">yes</span>
<span id="yn_2">no</span>
<span id="yn_3">hello</span>
<form onsubmit="return false">
<p><button onclick="alert(trueFalse('yn_1'));">span 1</button>
<button onclick="alert(trueFalse('yn_2'));">span 2</button>
<button onclick="alert(trueFalse('yn_3'));">span 3</button>
<button onclick="alert(trueFalse('yn_4'));">span 4</button>
</form>
</html>
 
R

RobG

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'))!='undefined') {

typeof is an operator, not a function - you don't need to wrap its
argument in brackets.

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-Core/core.html#ID-getElBId >
 
R

RobG

(e-mail address removed) a écrit :



<html>
<script type="text/javascript">
function trueFalse(who) {
if( document.getElementById(who) ) {  // does the span exist ?
     var value = document.getElementById(who).innerHTML;
         // does the span contain 'yes' or 'no' ?

It would be better to use the element's textContent or innerText
rather than innerHTML.

     if(value.indexOf('yes')>=0 || value.indexOf('no')>=0)

A regular expression testing for the words "yes" or "no" would make a
better test - the string "no" could exist quite easily as a part of
some other string, especially if the innerHTML property is used.

So:

if (/\byes\b|\bno\b/i).test(value))

will test if either of the words yes or no are in the string.
         // if it does return 'yes is the value ?'
     return value.indexOf('yes')>=0;  // that breaks here if ok

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).
 
S

SAM

RobG a écrit :
It would be better to use the element's textContent or innerText
rather than innerHTML.

Probably ?
Never I use the text elements while the html one can also do the job.
Is there a very bad practice ?
A regular expression testing for the words "yes" or "no" would make a
better test - the string "no" could exist quite easily as a part of
some other string, especially if the innerHTML property is used.

You're absolutely right
-1- I hope there is not a full book in one of the spans
-2- I don't know if one of the spans couldn't have more than one word
and however be accepted
-3- Step after step
(I think it is enough difficult for the OP without reg expressions)
So:

if (/\byes\b|\bno\b/i).test(value))

will test if either of the words yes or no are in the string.

We'll have to go to see what that is supposed to do.

\b = limit of word (space or other)
/ = limit of expression to search (limit of mask)
i = case insensitive


RegExp( mask, flags)

var Q = new RegExp( "\\bYES\\b|\\bNO\\b", "i");

if(Q.test(value))

<http://developer.mozilla.org/en/docs/Core_JavaScript_1.5_Reference:Global_Objects:RegExp>
or :
<http://developer.mozilla.org/en/docs/Core_JavaScript_1.5_Guide:Regular_Expressions>
then :
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).

I made a function that could return : true ou false or error
but, as you said, a function working as confirm() sending back 'true'
if it is ok and 'false' in other cases would answer to the purpose.
 
T

Thomas 'PointedEars' Lahn

RobG said:
if(typeof(document.getElementById('shoe1'))!='undefined') {
[...]
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
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-Core/core.html#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
 
D

Dr J R Stockton

In comp.lang.javascript message <e86c6aba-c140-4fdd-8083-1b95f681ef2a@h1
1g2000prf.googlegroups.com>, Sat, 15 Mar 2008 18:36:48, RobG
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.
 
R

RobG

RobG said:
if(typeof(document.getElementById('shoe1'))!='undefined') {
[...]
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.
 
R

RobG

In comp.lang.javascript message <e86c6aba-c140-4fdd-8083-1b95f681ef2a@h1
1g2000prf.googlegroups.com>, Sat, 15 Mar 2008 18:36:48, RobG
<[email protected]> posted:




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.
 
T

Thomas 'PointedEars' Lahn

RobG said:
RobG said:
On Mar 16, 10:10 am, (e-mail address removed) wrote:
if(typeof(document.getElementById('shoe1'))!='undefined') { [...]
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.
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.
It says Null is a primitive,

Certainly it does not.
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top