I’m using the
event.target.className
to get the ClassName, but sometimes an element has multiple class names, how can I make it,
so it only gives the first class name as outcome?
Oh, and please without jQuery.
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
There are various ways to get first class of an element
Using the className property of DOM elements with the split function which will split className by spaces and return an array.
The className property is supported in all major browsers.
Using the classList property of DOM elements which return a DOMTokenList Object of classes(already split by space)
The classList property is relatively new and relatively faster too.
This is not supported in IE8 and IE9. Support in various browsers
Check performance of classList vs className