Skip to content

24 ways to impress your friends

Vote down?

Andrew Phillipo

One of the ways of adding data to your markup that can be quite useful is being able to grab database information such as ids from classes. Here is a generic function for doing this (I can’t find a way to get pre blocks working on here, which makes this look messy…):

function getNumberFromClassName(el, prefix) { var regexp = new RegExp(prefix + ‘(\\d+)’), matches = el.className.match(regexp), result = (matches1) ? matches1 : false; return result;
}

Markup:

<a href=”/somewhere” class=“random i-14555”>My Link</a>

Call the function:

var number = getNumberFromClassName(myLink, ‘i-’);
// number equals 14555

Hope this is useful to someone… sorry textile bc. codeblocks do not work here…