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?
The parseInt
function always starts from the left side of the string. Try this:
var i = parseInt(yourString.replace(/\D/g, ''), 10);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With