Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You'd use the native <code>.className</code> property.</p> <pre><code>this.className </code></pre> <hr> <p>It's <code>this.className</code> instead of <code>this.class</code> because <code>class</code> is a reserved word in JavaScript, and some browsers haven't allowed reserved words to be used as property names.</p> <hr> <p>It sounds like there could be multiple classes on an element. To isolate one, you can use <code>$.grep</code> with <code>$.inArray</code>.</p> <pre><code>var classes = ['one','two','three']; $('.' + classes.join(',.')).click(function(){ var classNames = this.className.split(/\s+/); var cls = $.grep(classNames, function(c, i) { return $.inArray(c, classes) !== -1; })[0]; alert(cls); }); </code></pre> <p><em><strong>DEMO:</em></strong> <a href="http://jsfiddle.net/rFT8j/">http://jsfiddle.net/rFT8j/</a></p> <hr> <p>Or you could use <code>$.each</code> instead of <code>$.grep</code>.</p> <pre><code>var classes = ['one','two','three']; $('.' + classes.join(',.')).click(function(){ var classNames = this.className.split(/\s+/); var cls; $.each(classNames, function(i, c) { if( $.inArray(c, classes) !== -1 ) { cls = c; return false; } }); alert(cls); }); </code></pre> <p><em><strong>DEMO:</em></strong> <a href="http://jsfiddle.net/rFT8j/1/">http://jsfiddle.net/rFT8j/1/</a></p> <hr> <p>If you want something a little simpler, one solution would be to take advantage of closures, and assign separate handlers that each reference a different name in the variable scope...</p> <pre><code>var classes = ['one','two','three']; $.each(classes, function(i, c) { $('.' + c).click(function(){ alert(c); }); }); </code></pre> <p><em><strong>DEMO:</em></strong> <a href="http://jsfiddle.net/rFT8j/3/">http://jsfiddle.net/rFT8j/3/</a></p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload