21

I just want a very handy way to extract the numbers out of a string in Javascript and I am thinking about using jQuery, but I prefer the method that proves to be the simplest. I have requested the "left" attribute of a css block using jQuery like this:

var stuff = $('#block').css("left")

The result of "stuff" is

1008px

I just want to get rid of the "px" because I need to do a parseInt of it. What is the best method? If Javascript had a left() function, this would be very simple. Thanks

6 Answers 6

31

To answer your other question, you can add a left() function to JavaScript's built-in String prototype class so all other strings will inherit it:

String.prototype.left = function(n) {
    return this.substring(0, n);
}

And once you include this you can say:

var num = "1008px".left(4);

I add helpers like trim and capitalize in a base JavaScript file for these kinds of things.

3
  • fwiw, the native substr() is equivalent to left(), but I'm all for adding cheap flexibility
    – annakata
    Commented Jan 29, 2009 at 13:12
  • @annakata: substr() has not been standardized by ECMAScript and is thus deprecated. And still, you'd need to specify the start and length, but you are right, left() is just a convenience.
    – davidavr
    Commented Jan 29, 2009 at 13:27
  • There also is slice (15.5.4.13)
    – some
    Commented Jan 29, 2009 at 23:29
28

Just do a parseInt("1008px", 10), it will ignore the 'px' for you.

3
  • 11
    parseInt(stuff,10); //Don't forget the radix
    – some
    Commented Jan 29, 2009 at 11:31
  • What is the 10?
    – egmfrs
    Commented Jun 5 at 14:18
  • the radix Commented Jun 5 at 17:19
10

$('#block').offset().left contains the actual left position value as an integer.

2
  • 4
    That actually returns a different value - it returns the left position of the element relative to the document, not the css value for left set to the element.
    – hora
    Commented Oct 18, 2011 at 22:39
  • Besides, "jQuery does not support getting the offset coordinates of hidden elements or accounting for borders, margins, or padding set on the body element. While it is possible to get the coordinates of elements with visibility:hidden set, display:none is excluded from the rendering tree and thus has a position that is undefined." (api.jquery.com/offset)
    – antoine
    Commented Feb 24, 2015 at 21:56
3

Left() is of almost no use here, as you'd first have to calculate the offset. You could, however, use a regular expression, to either pull out the number, or delete illegal characters:

var value = "1080px";
var num = value.replace(/[^\d]+/g, '');
// or
var num = value.replace(/\D+/g, '');
// or
var num = value.match(/\d+/)[0];

That is, in case parseInt() isn't enough for you... :)

1
  • Using parseInt() on any string with letters in it (along with the number you're seeking) gives NaN, just FYI.
    – vapcguy
    Commented Jul 21, 2021 at 23:45
3

If you want to have that number without 'px' try it:

$('#block').position().left

That would be simplest on JQuery )

2
parseInt(stuff, 10);

edited to use the radix comment.

parseInt(stuff,10); //Don't forget the radix – some Jan 29 '09 at 11:32

5
  • 4
    parseInt(stuff,10); //Don't forget the radix
    – some
    Commented Jan 29, 2009 at 11:32
  • Using parseInt() on any string with letters in it (along with the number you're seeking) gives NaN.
    – vapcguy
    Commented Jul 21, 2021 at 23:46
  • 1
    Whoa this question is 12 years old! Are you sure using parseInt on a string with letters always gives NaN? jsfiddle.net/mgb84k3h
    – John Boker
    Commented Jul 22, 2021 at 14:31
  • @JohnBoker Running your fiddle, even after adding a semi-colon after your alert, doesn't give an alert, so not sure about that - but when I did a console.log(parseInt("1024px")); I got the desired 1024 output. But if I did console.log(parseInt("country1")); - I get NaN instead of 1. I thought maybe it only worked for px values, but I did console.log(parseInt("1024asd")); and got 1024 as expected. It's weird that it will work in some instances, but not others, so I wanted to call attention to that.
    – vapcguy
    Commented Jul 29, 2021 at 19:24
  • It looks like when the number part is at the beginning of the string it works.
    – John Boker
    Commented Jul 29, 2021 at 19:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.