By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,317 Members | 1,290 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,317 IT Pros & Developers. It's quick & easy.

Checking if span object(s) exist

P: n/a
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
Jul 20 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
In article <2b**************************@posting.google.com >,
an********@yahoo.com enlightened us with...
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
-------------------------------------------------
Jul 20 '05 #2

P: n/a
an********@yahoo.com (Uzi G.) writes:
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
--
Lasse Reichstein Nielsen - lr*@hotpop.com
Art D'HTML: <URL:http://www.infimum.dk/HTML/randomArtSplit.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 20 '05 #3

P: n/a
Lasse Reichstein Nielsen wrote:
an********@yahoo.com (Uzi G.) writes:
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.


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 <gw*****@agricoreunited.com>

* Client-side Javascript and Netscape 4 DOM Reference available at:
*
http://devedge.netscape.com/library/...ce/frames.html

* Internet Explorer DOM Reference available at:
*
http://msdn.microsoft.com/workshop/a...ence_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-deve...upgrade_2.html
Jul 20 '05 #4

P: n/a
You can use:

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

I hope this helps.

-Wagner
Jul 20 '05 #5

P: n/a
Grant Wagner <gw*****@agricoreunited.com> writes:
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
--
Lasse Reichstein Nielsen - lr*@hotpop.com
Art D'HTML: <URL:http://www.infimum.dk/HTML/randomArtSplit.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 20 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.