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
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":
You can use jQuery to check for classes by name:
and to add or remove them with addClass and removeClass.
This works for me. There is no event.target.class function in jQuery.
Try:
You will get all the class in below array
If you are using jQuery 1.7:
or:
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 likethus leading to
Thx for the nice answers!