I want to get the value of the class attribute of an element.
<a href="http://stackoverflow.com/" id="myId" class="myClassName">Click Me</a>
this.id, this.href and this.text is working.
My Question is why this.class is not working ?
Note:
I don't want to use:
console.log($this.attr('class')); or console.log($("a").prop("class"));
because its very slow.
$(function(){
$("a").on("click",function(){
console.log(this.id); // myId
console.log(this.href); // http://stackoverflow.com/
console.log(this.text); // Click Me
console.log($("a").prop("class")); // myClassName
});
});
Because it should be this.className instead.
REF: https://developer.mozilla.org/en-US/docs/DOM/element.className
use this.className it is native javascript element property.
$(function(){
$("a").on("click",function(){
console.log(this.id); // myId
console.log(this.href); // http://stackoverflow.com/
console.log(this.text); // Click Me
console.log(this.className); // myClassName
});
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With