Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery + parseInt() not playing nicely

Tags:

jquery

I have this snippet of jQuery used to get an ID number from an input field

$('table th input').change(function() {
    var id = $(this).attr('id');
    id = parseInt(id);
    id = isNaN(id) ? 0 : id;
    alert(id);
});

the ID's of the fields are along the lines of 'col2Name' etc, and I want to just grab the 2 from there, for some reason in my alert i am always getting 0, now when i try to just do:

alert(parseInt('12978sdkjfhakj'));

I get the appropriate response of 12978, why is this not working?

like image 844
Jimmy Avatar asked Feb 10 '10 21:02

Jimmy


1 Answers

The parseInt function always starts from the left side of the string. Try this:

var i = parseInt(yourString.replace(/\D/g, ''), 10);
like image 114
Pointy Avatar answered Nov 11 '22 14:11

Pointy