39

I have an element in javascript like follows:

 <span>280ms</span>

I want to extract 280 from the span element. How can I do it? The content within the span element will be any number followed by ms.

Pratik Gadoya
  • 1,420
  • 1
  • 16
  • 27
Saurabh Kumar
  • 16,353
  • 49
  • 133
  • 212

9 Answers9

84

parseInt() is pretty sweet.

HTML

<span id="foo">280ms</span>

JS

var text = $('#foo').text();
var number = parseInt(text, 10);
alert(number);

parseInt() will process any string as a number and stop when it reaches a non-numeric character. In this case the m in 280ms. After have found the digits 2, 8, and 0, evaluates those digits as base 10 (that second argument) and returns the number value 280. Note this is an actual number and not a string.

Edit:
@Alex Wayne's comment.
Just filter out the non numeric characters first.

parseInt('ms120'.replace(/[^0-9\.]/g, ''), 10);
HoldOffHunger
  • 18,769
  • 10
  • 104
  • 133
Alex Wayne
  • 178,991
  • 47
  • 309
  • 337
  • 9
    This is the best answer. Note that though the 10 parameter in `parseInt` is optional, you should use it to prevent accidental parsing of a number as octal. If the span contains "0700ms", that will not return 700 as a result but will be 448! – Jacob Aug 11 '11 at 21:58
  • 2
    @Jacob is right. 10 is not the default if you omit it. Instead javascript tries to guess based on the string format. History has proven that making javascript try to guess anything leads to pain and suffering. So, always explicitly call out your radix. – Alex Wayne Aug 11 '11 at 22:29
  • +1 `parseInt()` is the right tool, and you provided the `radix` parameter. – JAAulde Aug 12 '11 at 15:27
  • 1
    What about parsing ms120 to get 120? – Thomas Clowes Jul 12 '13 at 22:17
  • 7
    @ThomasClowes Just filter out the non numeric characters first. `parseInt('ms120'.replace(/[^0-9\.]/g, ''), 10);` – Alex Wayne Jul 12 '13 at 22:21
  • 1
    @AlexWayne You should include your last comment as part of answer. A simple `parseInt()` won't work if the string doesn't begin with number like already noted in comments. – Fr0zenFyr Jul 17 '15 at 08:07
  • @Alex Wayne Your comment should be the answer! – Jinbom Heo Nov 03 '15 at 06:08
  • If you're using regex to "filter out the non-numeric characters first", then you might as well just do everything with regex and obviate the need for `parseInt()` entirely: `'ms120'.match(/\d+/);` – rinogo Aug 17 '17 at 15:29
19

Try this:

var num = document.getElementById('spanID').innerText.match(/\d+/)[0];

jQuery version:

var num = $('span').text().match(/\d+/)[0]; // or $('#spanID') to get to the span

If you want as numeric value (and not as string), use parseInt:

var num = parseInt($('span').text().match(/\d+/)[0], 10);
Mrchief
  • 75,126
  • 20
  • 142
  • 189
  • 2
    Using `parseInt()`, you don't need the `regex` matching. Kudos for providing the `radix` parameter, though. – JAAulde Aug 12 '11 at 15:28
12

Try the following

var strValue = // get 280m from the span
var intValue = parseInt(strValue.match(/[0-9]+/)[0], 10);
JaredPar
  • 733,204
  • 149
  • 1,241
  • 1,454
4

You could use the parseInt() function

var number = parseInt($("span").text())
nip3o
  • 3,346
  • 4
  • 24
  • 29
0

in general for numbers no mather negative or positive

<div>
  blah blah
  <span>285blahblah</span>
</div>

var html= document.getElementsByTagName('div')[0].innerHTML;// or $('div').html() if jquery

var number = parseFloat(html.match(/-*[0-9]+/));

http://jsfiddle.net/R55mx/

h0mayun
  • 3,466
  • 31
  • 40
0

var myNumber= $('span').text().replace(/[^d.,]+/,'');

Themer
  • 584
  • 4
  • 9
0

Change the span in:

<span id='msSpan'>280ms</span>

Then you can do:

alert($('#msSpan').text());
slayerIQ
  • 1,488
  • 2
  • 13
  • 25
0

Will it always end in "ms"? You can do:

var num = s.substring(0, s.length-2) 

where s is the string in the span. To get this value, you can use text(), html(), or innerHTML on the span.

codecraftnap
  • 1,553
  • 2
  • 9
  • 9
-1

using jquery is pretty simple. probably better giving the span an id though

var mytext=replace($('span').text(),"ms","");

edited to remove ms

Johnny Craig
  • 4,974
  • 2
  • 26
  • 27