tags:

views:

259

answers:

1

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?

+5  A: 

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

var i = parseInt(yourString.replace(/\D/g, ''), 10);
Pointy
beautiful, thanks, you've saved me a huge headache
Jimmy