Skip to content

Instantly share code, notes, and snippets.

@yoko
Created June 1, 2009 09:43
Show Gist options
  • Save yoko/121326 to your computer and use it in GitHub Desktop.
Save yoko/121326 to your computer and use it in GitHub Desktop.
$('<div id="id-1234"/>').getId(); /* => 1234 */ $('<div id="id-foo"/>').getId(/-(\w+)$/); /* => foo */
(function($) {
$.fn.getId = function(pattern) {
pattern = pattern || /(\d+)/;
var id = this.attr('id');
if (!id) return null;
var ids = (pattern.exec(id) || []).slice(1);
return (ids.length > 1 ? ids : ids[0]) || null;
};
})(jQuery);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment