2

If I have

<p id="target">
  <span><sup>$</sup>50</span>
</p>

var $p = $("p#target");

How do I get just the text 50 from $p i.e. without the dollar sign?

moey
  • 10,587
  • 25
  • 68
  • 112
  • possible duplicate of [JQuery InnerText not including sub element](http://stackoverflow.com/questions/1476787/jquery-innertext-not-including-sub-element) – Felix Kling Jan 03 '12 at 01:02

4 Answers4

4
var num = $("#target > span")[0].lastChild.data;

http://jsfiddle.net/FJmLd/

1

Here's one way:

var clone = $("#target").clone();
clone.find("sup").remove();
var text = clone.text();

Demo: http://jsfiddle.net/ANp9y/

...or, if it's only ever a dollar you need to worry about:

var text = $.trim($("#target").text().replace("$", ""));
karim79
  • 339,989
  • 67
  • 413
  • 406
0

You could do this

var a = $('#target span').text();
var b = a.split('$');
alert(b[1]);

Example: http://jsfiddle.net/2mgeC/

Jason Gennaro
  • 34,535
  • 8
  • 65
  • 86
0

You can iterate over the contents and concatenate the trimmed text nodes:

var text = $("#target span").contents().map(function() {
    if(this.nodeType == Node.TEXT_NODE){
        return $.trim(this.nodeValue);
    }
}).toArray().join();
alert(text); //50

JSFiddle

Dennis
  • 32,200
  • 11
  • 64
  • 79