SO - 19001883

by Arun P Johny

HTML

<div class="button 17-facebook-dashboard-check">Elem1</div>
<div class="button 18-google-dashboard-check someother">Elem2</div>
<div class="button 19-twitter-dashboard-check">Elem3</div>
<div class="button">Elem4</div>

JavaScript

(function () {
    $.fn.hasClassEndsWith = function (suffix) {
        if (this.length === 0) {
            return false;
        }

        var clazz = this.attr('class');
        if (clazz === undefined) {
            return false;
        }

        var classes = clazz.split(' ');
        var regex = new RegExp(suffix + '$', 'i');
        for (var i = 0; i < classes.length; i++) {
            if (regex.test(classes[i])) {
                return true;
            }
        }
        return false;

    };
})(jQuery);

jQuery(function () {
    $('.button').click(function () {
        if ($(this).hasClassEndsWith('dashboard-check')) {
            console.log('dashboard')
        } else {
            console.log('normal')
        }
    });
})