Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > How to check if a named SPAN element exists (IE)?

Reply
Thread Tools

How to check if a named SPAN element exists (IE)?

 
 
Mike
Guest
Posts: n/a
 
      01-15-2004
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 < 20; x++) {
document.all('mySpan'+x).style.display = "inline";
}

But I donīt know how many SPAN elements there are, so I need to set x to a
value big enough to change them all. This does, however, produce a error
when the function tries to set "style.display" on a SPAN that doesnīt exist.
So I need to check if the SPAN element does exist before I try to set itīs
"style.display".

for (var x = 1; x < 20; x++) {
If (document.all('mySpan'+x)) <-- something like this (but that works
document.all('mySpan'+x).style.display = "inline";
}

But how do I do this? Help, anyone?

/Mike


 
Reply With Quote
 
 
 
 
Richard Cornford
Guest
Posts: n/a
 
      01-15-2004
"Mike" <> wrote in message
news:7wuNb.78331$...
>Iīve got a number of SPAN elements named "mySpan1", "mySpan2",
>"mySpan3" etc,


When you say "named" do you mean they have NAME attributes? The NAME
attribute is not valid on SPAN elements according to the HTML 4 DTDs.
But as the example strings conform to the requirements of an ID
attribute and ID attributes are valid on all elements, it would make
sense to be using an ID attribute.

>and want to set their "style.display" to "inline".


The default display property of a SPAN element is "inline", but maybe
you have pre-set them to "none" in the CSS.

> This works (only needs to work on IE5.5+):


Intranet or broken by design (in the sense that there is nothing here
that would prevent this form being implemented 100% successfully on
every modern dynamic visual browser (and then some))? It is often
helpful to go into the context in which you are working when asking
questions here.

> for (var x = 1; x < 20; x++) {
> document.all('mySpan'+x).style.display = "inline";


IE blurs the distinction between objects and function when dealing with
host objects and method. Native code method report that they or "object"
when tested with the typeof operator and DOM elements and collections
appear to accept property look-ups in the form of parameterised function
calls. But the document.all collection is best thought of as an object,
and so references to named properties of that object should use the
standard ECMA Script (which JScript claims to be an implementation of)
square bracket notation property accessor syntax:-

document.all['mySpan'+x].style.display = "inline";

It is difficult to say why IE allows parenthesise to be used in place of
property accessors to achieve the same task. It might be an attempt to
make scripting easier for people who don't really understand what they
are doing (tolerate what would be errors in other, stricter,
environments). But habitually using parentheses instead of square
brackets when accessing DOM object properties is one of the things that
makes IE centred script authors convinced that cross-browser scripting
is harder than it really is. If they had just learnt to use the proper
language feature in the first place they would find that a lot of IE
only scripts would work on many other browsers unaltered.

<URL: http://jibbering.com/faq/#FAQ4_39 >

> }
>
>But I donīt know how many SPAN elements there are,


Why not, where are they coming from? If they are generated by
server-side processes then you do know how many there are and are in a
position to tell the JScript.

>so I need to set x to a value big enough to change them all.


Surly setting - x - to a "big enough" value is of no use at all. It is
the - 20 - that decides how many elements you check, but that is
probably what you mean. If you have a server-script generating the SPAN
elements it should be in a position to write a value in place of that -
20 - based on the number of SPANs included on the page. Unless the
JScript is in a separate static file, but even then the server script
could create a global variable on the page that could be referred to in
place of the - 20 - in the external file.

>This does, however, produce a error when the function
>tries to set "style.display" on a SPAN that doesnīt exist.


It would.

>So I need to check if the SPAN element does exist before I
>try to set itīs "style.display".
>
>for (var x = 1; x < 20; x++) {
>If (document.all('mySpan'+x))<--something like this (but that works


Apart from the parenthesised property access (which usually works on IE)
the only reason for that not working is the initial capital "I " in
the - if - statement. ECMA Script, being case sensitive, would take that
as an identifier and treat the expression as an attempt to execute a
function referred to by that identifier. Erroring because there probably
is no - If - variable.

if(document.all['mySpan'+x]){
... // it exists! (or at least, whatever it refers
// to type-converts to boolean true, which should
// be the same thing where properties of the
// document.all collection are concerned).
}

>document.all('mySpan'+x).style.display = "inline";
>}
>
>But how do I do this? Help, anyone?


Because SPAN elements should not have NAME attributes in valid HTML 4
but can have ID attributes it would probably be best to use the ID
attribute (if you are not already). As a result it would be possible to
use the W3C DOM document.getElementById method to look up the reference
to the SPAN element using that ID attribute. IE 5.0+ implements the
getElementById method and it is a good habit to use W3C DOM methods
where they are implemented by IE instead of equivalent IE proprietary
features (document.all in this case). As it happens, it appears that IE
implements getElementById so that it looks up the element in the
document.all collection anyway, but habitually preferring the most
widely supported method even when writing exclusively for IE makes
writing cross-browser code easier at a later date.

The "big enough" number strategy doesn't seem like a good idea at all.
The number is going to have to be big enough to account for all possible
numbers of SPAN elements else it will fail, and that means a loop that
does a lot of work and achieves nothing for most of the time when there
are only a small number of SPAN elements.

It would probably be reasonable to - break - the loop as soon as the
first 'mySpan'+x string failed to find a corresponding SPAN (assuming
that the spans are sequentially numbered without interruptions). But
then a - for - loop might not be the best option, probably a - while -
loop would be better suited to the situation.

var spanEl, count = 1;
while(spanEl = document.getElementById('mySpan' + count++)){
spanEl.style.display = 'inline';
}

- would start with "mySpan1" and keep going until it could not find
"mySpan"+n.

Richard.


 
Reply With Quote
 
 
 
 
Mike
Guest
Posts: n/a
 
      01-15-2004
Wow,
Thanks a lot for that long and very indepth answer Richard!!

Really appreciate it - again, thx.

/Mike


> >Iīve got a number of SPAN elements named "mySpan1", "mySpan2",
> >"mySpan3" etc,

>
> When you say "named" do you mean they have NAME attributes? The NAME
> attribute is not valid on SPAN elements according to the HTML 4 DTDs.
> But as the example strings conform to the requirements of an ID
> attribute and ID attributes are valid on all elements, it would make
> sense to be using an ID attribute.
>
> >and want to set their "style.display" to "inline".

>
> The default display property of a SPAN element is "inline", but maybe
> you have pre-set them to "none" in the CSS.
>
> > This works (only needs to work on IE5.5+):

>
> Intranet or broken by design (in the sense that there is nothing here
> that would prevent this form being implemented 100% successfully on
> every modern dynamic visual browser (and then some))? It is often
> helpful to go into the context in which you are working when asking
> questions here.
>
> > for (var x = 1; x < 20; x++) {
> > document.all('mySpan'+x).style.display = "inline";

>
> IE blurs the distinction between objects and function when dealing with
> host objects and method. Native code method report that they or "object"
> when tested with the typeof operator and DOM elements and collections
> appear to accept property look-ups in the form of parameterised function
> calls. But the document.all collection is best thought of as an object,
> and so references to named properties of that object should use the
> standard ECMA Script (which JScript claims to be an implementation of)
> square bracket notation property accessor syntax:-
>
> document.all['mySpan'+x].style.display = "inline";
>
> It is difficult to say why IE allows parenthesise to be used in place of
> property accessors to achieve the same task. It might be an attempt to
> make scripting easier for people who don't really understand what they
> are doing (tolerate what would be errors in other, stricter,
> environments). But habitually using parentheses instead of square
> brackets when accessing DOM object properties is one of the things that
> makes IE centred script authors convinced that cross-browser scripting
> is harder than it really is. If they had just learnt to use the proper
> language feature in the first place they would find that a lot of IE
> only scripts would work on many other browsers unaltered.
>
> <URL: http://jibbering.com/faq/#FAQ4_39 >
>
> > }
> >
> >But I donīt know how many SPAN elements there are,

>
> Why not, where are they coming from? If they are generated by
> server-side processes then you do know how many there are and are in a
> position to tell the JScript.
>
> >so I need to set x to a value big enough to change them all.

>
> Surly setting - x - to a "big enough" value is of no use at all. It is
> the - 20 - that decides how many elements you check, but that is
> probably what you mean. If you have a server-script generating the SPAN
> elements it should be in a position to write a value in place of that -
> 20 - based on the number of SPANs included on the page. Unless the
> JScript is in a separate static file, but even then the server script
> could create a global variable on the page that could be referred to in
> place of the - 20 - in the external file.
>
> >This does, however, produce a error when the function
> >tries to set "style.display" on a SPAN that doesnīt exist.

>
> It would.
>
> >So I need to check if the SPAN element does exist before I
> >try to set itīs "style.display".
> >
> >for (var x = 1; x < 20; x++) {
> >If (document.all('mySpan'+x))<--something like this (but that works

>
> Apart from the parenthesised property access (which usually works on IE)
> the only reason for that not working is the initial capital "I " in
> the - if - statement. ECMA Script, being case sensitive, would take that
> as an identifier and treat the expression as an attempt to execute a
> function referred to by that identifier. Erroring because there probably
> is no - If - variable.
>
> if(document.all['mySpan'+x]){
> ... // it exists! (or at least, whatever it refers
> // to type-converts to boolean true, which should
> // be the same thing where properties of the
> // document.all collection are concerned).
> }
>
> >document.all('mySpan'+x).style.display = "inline";
> >}
> >
> >But how do I do this? Help, anyone?

>
> Because SPAN elements should not have NAME attributes in valid HTML 4
> but can have ID attributes it would probably be best to use the ID
> attribute (if you are not already). As a result it would be possible to
> use the W3C DOM document.getElementById method to look up the reference
> to the SPAN element using that ID attribute. IE 5.0+ implements the
> getElementById method and it is a good habit to use W3C DOM methods
> where they are implemented by IE instead of equivalent IE proprietary
> features (document.all in this case). As it happens, it appears that IE
> implements getElementById so that it looks up the element in the
> document.all collection anyway, but habitually preferring the most
> widely supported method even when writing exclusively for IE makes
> writing cross-browser code easier at a later date.
>
> The "big enough" number strategy doesn't seem like a good idea at all.
> The number is going to have to be big enough to account for all possible
> numbers of SPAN elements else it will fail, and that means a loop that
> does a lot of work and achieves nothing for most of the time when there
> are only a small number of SPAN elements.
>
> It would probably be reasonable to - break - the loop as soon as the
> first 'mySpan'+x string failed to find a corresponding SPAN (assuming
> that the spans are sequentially numbered without interruptions). But
> then a - for - loop might not be the best option, probably a - while -
> loop would be better suited to the situation.
>
> var spanEl, count = 1;
> while(spanEl = document.getElementById('mySpan' + count++)){
> spanEl.style.display = 'inline';
> }
>
> - would start with "mySpan1" and keep going until it could not find
> "mySpan"+n.
>
> Richard.
>
>



 
Reply With Quote
 
 
 
Reply

Thread Tools

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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
Can't code a css class that makes a span element the same size as a button element Cal Who ASP .Net 5 04-26-2010 05:35 PM
I'm looking for html cleaner. Example : convert <h1><span><font>my title</font></span></h1> => <h1>my title</h1>… Stéphane Klein Python 2 03-30-2010 12:35 AM
How to check if a directory exists? folder.exists() does not work! Ulf Meinhardt Java 8 08-28-2009 12:26 PM
Can span include span? Fulio Open HTML 5 06-26-2009 10:24 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57