try it
var check = "whatever-"; $('[class^="whatever-"], [class*=" whatever-"]').each(function () { // Get array of class names var cls = $(this).attr('class').split(' '); for (var i = 0; i < cls.length; i++) { // Iterate over the class and log it if it matches if (cls[i].indexOf(check) > -1) { console.log(cls[i].slice(check.length, cls[i].length)); } } });
This should also work if there is more than one class. There may be cleaner ways to do this using the filter method and the regex bit.
Little cleaner using map
var check = "whatever-"; $('[class^="whatever-"], [class*=" whatever-"]').each(function () { var className = this.className; var cls = $.map(this.className.split(' '), function (val, i) { if (val.indexOf(check) > -1) { return val.slice(check.length, val.length) } }); console.log(cls.join(' ')); });
Show demo
source share