Getting the class of the element that fired an event using JQuery

JavascriptJqueryJquery Events

Javascript Problem Overview


is there anyway to get the class when click event is fired. My code as below, it only work for id but not class.

$(document).ready(function() {
  $("a").click(function(event) {
    alert(event.target.id + " and " + event.target.class);
  });
});

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<html>

<body>
  <a href="#" id="kana1" class="konbo">click me 1</a>
  <a href="#" id="kana2" class="kinta">click me 2</a>
</body>

</html>

jsfiddle code here

Javascript Solutions


Solution 1 - Javascript

Try:

$(document).ready(function() {
    $("a").click(function(event) {
       alert(event.target.id+" and "+$(event.target).attr('class'));
    });
});

Solution 2 - Javascript

This will contain the full class (which may be multiple space separated classes, if the element has more than one class). In your code it will contain either "konbo" or "kinta":

event.target.className

You can use jQuery to check for classes by name:

$(event.target).hasClass('konbo');

and to add or remove them with addClass and removeClass.

Solution 3 - Javascript

You will get all the class in below array

event.target.classList

Solution 4 - Javascript

$(document).ready(function() {
  $("a").click(function(event) {
    var myClass = $(this).attr("class");
    var myId = $(this).attr('id');
    alert(myClass + " " + myId);
  });
})

<html>

<head>
  <script type="text/javascript" src="http://code.jquery.com/jquery-1.7.2.min.js"></script>
</head>

<body>
  <a href="#" id="kana1" class="konbo">click me 1</a>
  <a href="#" id="kana2" class="kinta">click me 2</a>
</body>

</html>

This works for me. There is no event.target.class function in jQuery.

Solution 5 - Javascript

An improvement on Vishesh answer, which instead returns a Boolean:

event.target.classList.contains(className)

Solution 6 - Javascript

If you are using jQuery 1.7:

alert($(this).prop("class"));

or:

alert($(event.target).prop("class"));

Solution 7 - Javascript

Careful as target might not work with all browsers, it works well with Chrome, but I reckon Firefox (or IE/Edge, can't remember) is a bit different and uses srcElement. I usually do something like

var t = ev.srcElement || ev.target;

thus leading to

$(document).ready(function() {
    $("a").click(function(ev) {
        // get target depending on what API's in use
        var t = ev.srcElement || ev.target;

        alert(t.id+" and "+$(t).attr('class'));
    });
});

Thx for the nice answers!

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionRedboxView Question on Stackoverflow
Solution 1 - JavascriptbroeschView Answer on Stackoverflow
Solution 2 - JavascriptPaulView Answer on Stackoverflow
Solution 3 - JavascriptVishesh MishraView Answer on Stackoverflow
Solution 4 - JavascriptdrjayView Answer on Stackoverflow
Solution 5 - JavascriptOskar hertzmanView Answer on Stackoverflow
Solution 6 - JavascriptEvan MulawskiView Answer on Stackoverflow
Solution 7 - JavascriptRomView Answer on Stackoverflow