Hi,

> Should I be able to say the following?
>
>   var total = parseFloat($('foo'));

No, $() gives you -- as you discovered -- a reference to the Element
object for the span, not the text of its content.  It's an object with
lots of properties and methods.  To get the text of its content, you
have to go get it.  This HTML:

>   <span id='foo'>50</span>

Consists of two nodes:  A span element containing a text node.  The
text node's value is "50", so you *could* do this:

var total = parseFloat($('foo').firstChild.nodeValue);

That might meet your needs provided you know for sure that the HTML
will look exactly like that and not (for instance) like this for some
reason:

>   <span id='foo'><span class='numeric'>50</span></span>

or indeed this:

>   <span id='foo'></span>

...because in each case that's a different structure and the code
above would break.

If what you're looking for is the text content of the Element and all
of its children, bundled up together, there's the DOM3 'textContent'
property[1] supported by Firefox (and other Gecko-based browsers),
Opera, and Safari (at least, Safari on Windows).  IE6 doesn't support
it (don't know about IE7), but it has an equivalent called
innerText[2].  So you can test whether the browser provides
textContent, use it if so, and if not see if it provides innerText.

[1] http://www.w3.org/TR/DOM-Level-3-Core/core.html#Node3-textContent
[2] http://msdn.microsoft.com/en-us/library/ms533899(VS.85).aspx

Alternately, there's a very handy property called 'innerHTML' that's
not a W3C standard but is widely-supported; it was an IE thing that's
been picked up by other browsers.  It gives you the HTML
representation of everything inside the element, including the tags.
Now, in your situation you don't want the tags, but Prototype provides
a handy stripTags() method on the String object, and so:

var total = $('foo').innerHTML.stripTags();

...would give you the equivalent of textContent/innerHTML another way.

Finally, you could do a recursive descent with pure DOM methods
collecting the nodeValue of all text nodes you found.  But it would
probably be slower than the shortcuts provided by browsers.

Anyone out there have better approaches than textContent/innerText or
using innerHTML and stripping the tags?

HTH,
--
T.J. Crowder
tj / crowder software / com


On Nov 6, 2:06 am, Scott <[EMAIL PROTECTED]> wrote:
> If I have an element:
>
>   <span id='foo'>50</span>
>
> Should I be able to say the following?
>
>   var total = parseFloat($('foo'));
>
> Instead, I am getting a return value of:
>
>   [object HTMLSpanElement]
>
> and parseFloat is returning NaN.
>
> Thanks in advance.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Prototype & script.aculo.us" group.
To post to this group, send email to prototype-scriptaculous@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/prototype-scriptaculous?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to