JQuery + parseInt () doesn't play well

I have this jQuery snippet used to get the id from input field

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

The field ID matches the lines of 'col2Name', etc., and I just want to take 2 from there, for some reason in my warning I always get 0, now when I try to just do:

alert(parseInt('12978sdkjfhakj'));

I get the corresponding answer 12978, why does this not work?

+5
source share
1 answer

The function parseIntalways starts on the left side of the line. Try the following:

var i = parseInt(yourString.replace(/\D/g, ''), 10);
+17
source

All Articles