views:

131

answers:

4
+2  A: 

Are there multiple spans inside #count?

If so, you could try

$("#counts span:first").text()
Philippe Leybaert
no there is only one inside.
abdelkrimnet
Can you show a piece of the HTML you're talking about?
Philippe Leybaert
But you probably still only want the child of #count and not any descendant of count. $("#counts > span")
Daniel Papasian
In fact, it would be better to attach a class to the span you need and select it explicitly: $("#counts span.classname").text()
Philippe Leybaert
+1  A: 

The selector '#counts span' will pick up any <span> tags that are a child of #counts (or a children of its children, etc.), so my guess is that you're accidentally selecting more nodes than you meant to grab.

It's hard to say how to modify your XPATH query correctly without seeing the page's source, though.

Daniel Lew
jQuery is using CSS syntax, not XPATH. (Actually it used to support both XPath and CSS but XPath was removed because of the confusion and complexity of the parser.)
Vincent Robert
A: 

Try to use it as a number:

var valueAsInt = parseInt($('#counts span').text());

If that works you can add convert it to a string by adding an empty string at the end of the expressions.

If that doesn't work you have a number of ways to debug this. Try:

alert($('#counts span').text());

If that returns 10, the problem is in the parsing. If that returns 10000 then the element contains a different value than you think so you can try:

alert($('#counts span').parent().html());

and verify the HTML does indeed contain 10 inside the <span>.

aleemb
actually the next line of my script is converting to an integer and there where the probleme is.
abdelkrimnet
Don't use "parseInt" without specifying a radix
J-P
A: 

You can use $('#counts span').html(); and it will only return the innerHtml from the first match. The simplest solution would be to add a id to the span to make sure you get just what you want. $('#span-id').text(); or $('#span-id').html(); They should both return the same value at this point.

Scott
with the html function it works fine.
abdelkrimnet
I find it extremely hard to believe that .text() returns "100000000000", and html() returns "10".
Philippe Leybaert
.text() includes the contents of all matching elements where as .html() only includes the first matching element. This is why I recommended putting an id on the span so it could be referenced directly with either method.
Scott