jquery get number from id jquery get number from id jquery jquery

jquery get number from id


var id = $("div").attr('id').replace(/button/, '');


Your "id" values cannot be purely numeric; they need to start with a letter or "_" like an identifier. (Note: that's not true in HTML5 documents.) Once you've got a number in your "id" value like that, you would just use the jQuery attr() function to get the id:

 var theId = parseInt($('div.whatever').attr('id').replace(/[^\d]/g, ''), 10);


// replace all non-digits with nothingalert($('div').attr("id").replace(/\D/g,''));

If you like, you can just grab the numbers at the end:

// <div id="_34_foo_555"></div>alert($('div').attr("id").match(/\d+$/));// alerts "555"