Checking if span object(s) exist

U

Uzi G.

Hi,

We have a JSP website intended for viewing with IE 5 (and above)
On a certain jsp page, there are few span objects
(<SPAN ID="name">text</SPAN>), which are created dynamically.

The spans share the same ID, so we are able to reference them as an
array, in the javascript code :
for example:

<SPAN ID="wow">lala</SPAN>
<SPAN ID="wow")kaka</SPAN>

in the JS code:

wow[0].innerHTML = "fafa";
wow[1].innerHTML = "zaza";

It may occur, since these spans are created dynamically by the JSP
page, that on a certain instance of the page, no spans are created.

My question is - how do we know, in the JS code, that no spans of ID
'wow' exist?
We tried several ways:

if (!wow) alert('no wow'); // If there were no spans, this gives a
js error

tried a few other methods which i cant recall of now..
I know of a workaround - to add a 'dummy' span with the 'wow' ID , but
I prefer to know if there is a better way to deal with this.

Thanks in advance,
Uzi
 
K

kaeli

We tried several ways:

if (!wow) alert('no wow'); // If there were no spans, this gives a
js error

if (!document.getElementById("wow") || document.getElementById("wow") ==
null || document.getElementById("wow") == "undefined")
alert ('no wow');

--
-------------------------------------------------
~kaeli~
All I ask for is the chance to prove that money
cannot make me happy.
http://www.ipwebdesign.net/wildAtHeart
http://www.ipwebdesign.net/kaelisSpace
-------------------------------------------------
 
L

Lasse Reichstein Nielsen

We have a JSP website intended for viewing with IE 5 (and above)

I was just about to stop reading here.
On a certain jsp page, there are few span objects
(<SPAN ID="name">text</SPAN>), which are created dynamically.

The spans share the same ID, so we are able to reference them as an
array, in the javascript code :
for example:

<SPAN ID="wow">lala</SPAN>
<SPAN ID="wow")kaka</SPAN>

I assume the ")" is a type. The generated code is invalid HTML. The ID
attribute must be unique in a page. Anything else is asking for trouble.
.... but if IE allows it, I guess you can ignore correctness.
It may occur, since these spans are created dynamically by the JSP
page, that on a certain instance of the page, no spans are created.

My question is - how do we know, in the JS code, that no spans of ID
'wow' exist?

You are also using the IE-specific way of accessing the named
elements: as a global variables of the same name. That means that
if there are no spans with that id, there is no variable either.
We tried several ways:

if (!wow) alert('no wow'); // If there were no spans, this gives a
js error

Yes, because there is no global variable called "wow", and it is an error
to access an undeclared variable.
tried a few other methods which i cant recall of now..

Interesting. Almost any other check would work:

if (!document.getElementById("wow")) ...
if (!document.all["wow"]) ...
if (!window.wow) ...

(As an interesting side node: If you have two elements with the same id,
and use document.all[id], the returned "array" contains *two* properties
with the same property name (id). Highly spurious!)

/L
 
G

Grant Wagner

Lasse said:
I was just about to stop reading here.


I assume the ")" is a type. The generated code is invalid HTML. The ID
attribute must be unique in a page. Anything else is asking for trouble.
... but if IE allows it, I guess you can ignore correctness.


You are also using the IE-specific way of accessing the named
elements: as a global variables of the same name. That means that
if there are no spans with that id, there is no variable either.


Yes, because there is no global variable called "wow", and it is an error
to access an undeclared variable.

Use the "typeof" operator to test for the existance of a variable/object
property:

if (typeof wow == 'undefined') {
alert('no wow');
}

But as was pointed out earlier, you shouldn't have multiple elements on a
page with the same id, you'd be much better off using:

<span id="wow1">...</span>
<span id="wow2">...</span>
<!-- etc -->

--
| Grant Wagner <[email protected]>

* Client-side Javascript and Netscape 4 DOM Reference available at:
*
http://devedge.netscape.com/library/manuals/2000/javascript/1.3/reference/frames.html

* Internet Explorer DOM Reference available at:
*
http://msdn.microsoft.com/workshop/author/dhtml/reference/dhtml_reference_entry.asp

* Netscape 6/7 DOM Reference available at:
* http://www.mozilla.org/docs/dom/domref/
* Tips for upgrading JavaScript for Netscape 7 / Mozilla
* http://www.mozilla.org/docs/web-developer/upgrade_2.html
 
W

W d'Anjos

You can use:

if (!document.all.wow) alert('no wow');

I hope this helps.

-Wagner
 
L

Lasse Reichstein Nielsen

Grant Wagner said:
Use the "typeof" operator to test for the existance of a variable/object
property:

if (typeof wow == 'undefined') {
alert('no wow');
}

Interesting. I didn't know that "typeof" allowed undeclared variables,
but reading the ECMAScript specification, it does work as you say.
Thanks :)
/L
 

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,770
Messages
2,569,586
Members
45,083
Latest member
SylviaHarr

Latest Threads

Top