1

I have some elements such as:

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

and a handler on:

$('.button').click(function () { });

Inside this handler I need to do some special operations such as extrapolate NN and SOCIAL for the elements that have a class that finishes with -dashboard-check.

For example if I click on Elem4 nothing should happens. If I click on Elem1 I need to print 17 and facebook, both distinct in two variables.

What the fast way to do this on jquery? regex? .HasClass that endswith?

Urinprobe
  • 97
  • 1
  • 11
markzzz
  • 47,390
  • 120
  • 299
  • 507
  • 1
    Do you have control of the HTML? It would be *much* better to embed this information in `data-` attributes instead of classes. – Jon Sep 25 '13 at 10:05
  • That's so smart ;) If you put as answer, you will got the accepted answer. – markzzz Sep 25 '13 at 10:08

7 Answers7

4

Assuming you have control over the HTML, it would be much preferable to use HTML data- attributes to embed this information into:

<div class="button" data-id="17" data-service="facebook">Elem1</div>
<div class="button" data-id="18" data-service="google">Elem2</div>
<div class="button" data-id="19" data-service="twitter">Elem3</div>
<div class="button">Elem4</div>

It's now very easy to write the selector that only works on the elements you want:

$('.button[data-id][data-service]').click(...);

And also very easy to get the information you need on click:

$('.button[data-id][data-service]').click(function() {
    alert($(this).attr("data-id"));           // with jQuery
    alert(this.getAttribute("data-service")); // without jQuery
});
Jon
  • 428,835
  • 81
  • 738
  • 806
  • You could also offer the jQuery option of `$(this).data("id")` – Reinstate Monica Cellio Sep 25 '13 at 10:15
  • 1
    @Archer: True, but that option would also cache the data inside jQuery's private data store, which behaves slightly differently (for one, it's not kept in sync with the DOM). Since there doesn't seem to be a reason to do it I omitted it on purpose. – Jon Sep 25 '13 at 10:24
2

Use ends with selector

    $( "div[class$='-dashboard-check']" ).each(function( index ) {
       console.log( index + ": " + $( this ).attr("id") );
    });
Suresh Atta
  • 120,458
  • 37
  • 198
  • 307
0

Try using the endswith attribute selector:

$("[attribute$='value']")

i.e.

$("[class$='-dashboard-check']")
Urinprobe
  • 97
  • 1
  • 11
0

you need *=

Description: Selects elements that have the specified attribute with a value containing the a given substring.

try:

$( "div[class*='-dashboard-check']" )

$= check string ends with
*= check string contains

Here is full list of attribute selector

Zaheer Ahmed
  • 28,160
  • 11
  • 74
  • 110
0

You can use the [attribute$=value] CSS selector: http://www.w3schools.com/cssref/sel_attr_end.asp. Something like :[class$=endVal].

Stefan Dunn
  • 5,363
  • 7
  • 48
  • 84
0

Try

(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')
        }
    });
})

Demo: Fiddle

Arun P Johny
  • 384,651
  • 66
  • 527
  • 531
0
 <div class="button 17-facebook-dashboard-check">Elem1</div>
 <div class="button 18-google-dashboard-check">Elem2</div>
 <div class="button 19-twitter-dashboard-check">Elem3</div>
 <div class="button">Elem4</div>

Try this:

 $( "div[class$='-dashboard-check']" ).click(function (e) {
    var o = $(e.target)
    var str = o.attr('class');
    var s1 = str.substring(7,9);
    var s2 = str.substr(10,str.indexOf('-'));
    alert(s1+" "+s2);
  });
Jyoti Prakash
  • 1,180
  • 8
  • 9