473,385 Members | 1,445 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Checking if span object(s) exist

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
5 11055
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
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
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
You can use:

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

I hope this helps.

-Wagner
Jul 20 '05 #5
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

13
by: Mikko Ohtamaa | last post by:
From XML specification: The representation of an empty element is either a start-tag immediately followed by an end-tag, or an empty-element tag. (This means that <foo></foo> is equal to...
2
by: Mike | last post by:
I´ve got a number of SPAN elements named "mySpan1", "mySpan2", "mySpan3" etc, and want to set their "style.display" to "inline". This works (only needs to work on IE5.5+): for (var x = 1; x <...
12
by: Stan Brown | last post by:
I've been thinking about float-ing a span versus float-ing a div, and the same for absolute positioning. When what is floated or positioned is a short chunk of text, I don't see any _visual_...
4
by: blueey | last post by:
I have a span element somewhere somewhere in between my table. How do i loop through the elemenst (namely in this case radio and text) and retreive the values of the child elements and it's type....
6
by: hsomob1999 | last post by:
so i have a <ul> and I allow the user to append items to it. The problem is that on mozilla the <span class="line"> which is just a line to divide the sections gets overlaped and doesnt move down...
0
by: Mike Meyer | last post by:
The recent thread on threads caused me to reread the formal definition of SCOOP, and I noticed something I hadn't really impressed me the first time around: it's using staticly checkable rules to...
4
by: Darrel | last post by:
I'm trying to add an extra layer of error checking on a Drop Down List. The list is populated from one table, and then I select the selectedValue from another DB. While it SHOULDN'T ever happen,...
8
by: Fuzzydave | last post by:
Okay, I have been handed a python project and working through it I have had to add a report. I am returning 10 variables the results of an SQL Query and as usual the number of results vary from...
7
by: mavigozler | last post by:
IE7 does not appear to set an event on contained text inside SPAN elements whose 'onclick', 'onmouseover', and 'onmouseout' events, defying the HTML recommendation. Firefox appears to conform. ...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.