Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

access span text

0 views
Skip to first unread message

Simon

unread,
Mar 11, 2004, 8:31:32 AM3/11/04
to
Is there a way to access span text from javascript? I need to access all
elements on a page and can get most of them from document.form[x],
document.images[x] and document.links[x], but anything rendered outside of a
form in a <span> section does not seem to appear in document.

Thanks IA, Simon


Martin Honnen

unread,
Mar 11, 2004, 8:42:43 AM3/11/04
to

Simon wrote:

Well, with the W3C DOM as implemented in IE5+, Netscape 6+, Opera 7 you
can access any element by its id attribute e.g.
<span id="aSpan">Kibology</span>
then you can script
var span;
if (document.getElementById) {
span = document.getElementById('aSpan');
if (span) {
// use span here e.g.
// span.style.backgroundColor = 'lightblue';
}
}
As for accessing the span's content the easiest cross browser way is
probably
span.innerHTML
though the DOM in IE5+ and Opera 7 also provides
span.innerText
and the W3C DOM has several ways of accessing child nodes
--

Martin Honnen
http://JavaScript.FAQTs.com/

Michael Winter

unread,
Mar 11, 2004, 8:56:54 AM3/11/04
to
On Thu, 11 Mar 2004 14:42:43 +0100, Martin Honnen <maho...@yahoo.de>
wrote:

[snip]

> As for accessing the span's content the easiest cross browser way is
> probably
> span.innerHTML

Is that a cross-browser approach? I don't believe it is. It's just another
Microsoft invention that just happens to be implemented by the major
players in browser development. It hasn't been published in any standard
to date.

Mike

--
Michael Winter
M.Wi...@blueyonder.co.invalid (replace ".invalid" with ".uk" to reply)

Martin Honnen

unread,
Mar 11, 2004, 10:01:38 AM3/11/04
to

Michael Winter wrote:

> On Thu, 11 Mar 2004 14:42:43 +0100, Martin Honnen <maho...@yahoo.de>
> wrote:
>> As for accessing the span's content the easiest cross browser way is
>> probably
>> span.innerHTML
>
>
> Is that a cross-browser approach? I don't believe it is. It's just
> another Microsoft invention that just happens to be implemented by the
> major players in browser development. It hasn't been published in any
> standard to date.

Well,
var img = new Image();
img.src = 'whatever.gif';
is a cross browser approach of preloading an image without being
published in any standard either so I don't know how a standard helps.
We have SVG 1.0 and SVG 1.1 standards which doesn't mean you can serve
SVG to major browsers and they handle it just because it is a standard.
My post mentioned browsers for which innerHTML is a cross browser approach.

Michael Winter

unread,
Mar 11, 2004, 10:38:03 AM3/11/04
to
On Thu, 11 Mar 2004 16:01:38 +0100, Martin Honnen <maho...@yahoo.de>
wrote:

[snip]

> Well,
> var img = new Image();
> img.src = 'whatever.gif';
> is a cross browser approach of preloading an image without being
> published in any standard either so I don't know how a standard helps.
> We have SVG 1.0 and SVG 1.1 standards which doesn't mean you can serve
> SVG to major browsers and they handle it just because it is a standard.

That is a very good point, as exemplified by IE with...well most standards.

Don't get me wrong. I'm not trying to imply that standards are a way of
guaranteeing implementation. If that were the case, once a new standard is
released, you wouldn't need to write fallbacks for modern browsers. This
doesn't happen though. Even after the three years since DOM 2 Style was
released, Opera[1] still doesn't implement the styleSheets collection.

However, I would argue that it's misleading to rate a proprietory feature
as cross-browser as opposed to the equivalent W3C DOM approach, which is
more likely to be supported over a wider range of browsers.

> My post mentioned browsers for which innerHTML is a cross browser
> approach.

I read that as examples of browsers that supported the getElementById()
method, actually.

Mike


[1] I thought I'd give Microsoft a break, for a change.

Richard Cornford

unread,
Mar 11, 2004, 12:15:41 PM3/11/04
to
Michael Winter wrote:
<snip>

> However, I would argue that it's misleading to rate a proprietory
> feature as cross-browser as opposed to the equivalent W3C DOM
> approach, which is more likely to be supported over a wider range of
> browsers.

The proprietary innerHTML property has an unusual place in cross-browser
scripting because the modern dynamic browsers; IE, Mozilla/Gecko,
Safari/Konqueror, Opera and IceBrowser, all implement it dynamically in
addition to (sufficient) W3C DOM methods. So with IE 4 implementing
innerHTML but no W3C DOM methods the numbers stack up in favour of
innerHTML as the most cross-browser method of modifying DOM structure
and page contents.

The browsers that do not implement innerHTML but do implement some of
the W3C DOM methods (e.g. Opera 5 & 6, NetFront/Web Browser 2) tend not
to be dynamic so modifying the DOM or contents is not an option there
anyway.

But with a browser's javascript support not being sufficient to infer
the ability to dynamically modify the DOM' structure the problem with
either approach is matching the desire to use one of the techniques with
the browsers support for the feature.

<snip>


> [1] I thought I'd give Microsoft a break, for a change.

Haven't Microsoft been on a break from browser development for nearly
two years now? I think it is probably time they got back to it. ;-)

Richard.


Douglas Crockford

unread,
Mar 11, 2004, 12:41:29 PM3/11/04
to
>>[1] I thought I'd give Microsoft a break, for a change.

> Haven't Microsoft been on a break from browser development for nearly
> two years now? I think it is probably time they got back to it. ;-)

I strongly disagree with that. The release of IE 7 will be a very dark
day indeed. Microsoft's greatest service to the stability of the web has
been to not introduce new bugs and incompatibilities for two years.
Considering their history, this has been a significant accomplishment
for them. I would like very much for this to continue.

Bravo, Microsoft. Keep it not coming. No new bugs.

Richard Cornford

unread,
Mar 11, 2004, 1:08:03 PM3/11/04
to
Douglas Crockford wrote:
<snip>

LOL. The glass is half full; that is one way of looking at it.

Richard.


Simon

unread,
Mar 12, 2004, 4:45:10 AM3/12/04
to
> <span id="aSpan">Kibology</span>
> then you can script
> var span;
> if (document.getElementById) {
> span = document.getElementById('aSpan');
> if (span) {
> // use span here e.g.
> // span.style.backgroundColor = 'lightblue';
> }
> }

Probably should have mentioned that I have no control over the page content
so no idea of names/ids.

> As for accessing the span's content the easiest cross browser way is
> probably
> span.innerHTML
> though the DOM in IE5+ and Opera 7 also provides
> span.innerText
> and the W3C DOM has several ways of accessing child nodes
> --

Thanks for that Martin. I found the most effective way to look through an
unknown page's elements is using document.links, document.forms[x].children
and document.body.children. Seems to work for all the elements on the pages
I use so far.

Simon


Imran Salahuddin Khan

unread,
Mar 12, 2004, 5:38:12 AM3/12/04
to
if this is the span ...
<span id="span1">ABCD</span>
then you can write in javascript
var spantext=document.getElementById("span1").innerText;
alert(spantext); // or any other use...


!mran Khan

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!

Martin Honnen

unread,
Mar 12, 2004, 6:42:55 AM3/12/04
to

Simon wrote:


>> if (document.getElementById) {
>> span = document.getElementById('aSpan');
>

> Probably should have mentioned that I have no control over the page content
> so no idea of names/ids.

Then you can't find elements by id but the DOM offers other ways,
var spans = document.getElementsByTagName('span');
that yields a collection with all <span> elements in the document with
IE5+, Netscape 6+, Opera 6+.

>>As for accessing the span's content the easiest cross browser way is
>>probably
>> span.innerHTML
>>though the DOM in IE5+ and Opera 7 also provides
>> span.innerText
>>and the W3C DOM has several ways of accessing child nodes
>>--
>
>
> Thanks for that Martin. I found the most effective way to look through an
> unknown page's elements is using document.links, document.forms[x].children
> and document.body.children. Seems to work for all the elements on the pages
> I use so far.

No, forget about children, that is IE only, if you want to write cross
browser code use
document.getElementsByTagName('tagname')
element.getElementsByTagName('tagname')
and
node.childNodes

Randy Webb

unread,
Mar 12, 2004, 8:15:53 AM3/12/04
to
Imran Salahuddin Khan wrote:
> if this is the span ...
> <span id="span1">ABCD</span>
> then you can write in javascript
> var spantext=document.getElementById("span1").innerText;
> alert(spantext); // or any other use...

And then wonder why it only works in IE? .innerText is IE-only.
innerHTML is at least more cross-browser.


--
Randy
Chance Favors The Prepared Mind
comp.lang.javascript FAQ - http://jibbering.com/faq/

Simon

unread,
Mar 12, 2004, 12:54:59 PM3/12/04
to
> Then you can't find elements by id but the DOM offers other ways,
> var spans = document.getElementsByTagName('span');
> that yields a collection with all <span> elements in the document with
> IE5+, Netscape 6+, Opera 6+.
>

Thanks - I use getElementsByTagName now and it works just fine.


0 new messages