I have an element in javascript like follows:
280ms
I want to extract 280 from the span element. How can I do it?
You could use the parseInt()
function
var number = parseInt($("span").text())
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/
Try the following
var strValue = // get 280m from the span
var intValue = parseInt(strValue.match(/[0-9]+/)[0], 10);
Change the span in:
<span id='msSpan'>280ms</span>
Then you can do:
alert($('#msSpan').text());
var myNumber= $('span').text().replace(/[^d.,]+/,'');
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);