To obtain the name of the element class?

Hello! Please help. I do not understand why andefined
$("#menu_open .col-md-3 div").each(function(e) { 
$(this).hover(function () { 
 let a = $(this).attr('class');
console.log(a);
})

});


Thank you!
March 23rd 20 at 19:19
2 answers
March 23rd 20 at 19:21
Solution
Because a div doesn't have an attribute class?

https://codepen.io/felicita46/pen/OYYEaE?editors=1111
Please explain
<div class ' ="cl2"></div> - tamara_Barrows commented on March 23rd 20 at 19:24
@tamara_Barrowsif you copied it from your code, then you have the div tag the class attribute is the Russian letter C - Anita commented on March 23rd 20 at 19:27
@tamara_Barrows, if we have this kind of div, then everything is OK. by Itself, the design of the class="" assumes that the diva has this attribute. Otherwise, it is either empty, like I gave in example class=""or undefined. You're not trying to appeal to this marvel and find it an attribute, e.g. data-width or style? Right, because they had not. Therefore, the appeal to a simple <div> will give undefined.

Distribute clearly in the first reply.

If your script returns undefined, and the DIVS have a class attribute then you need to look carefully. It may well be that you are a sample, not on those elements. Check design and connection within it. - felicita46 commented on March 23rd 20 at 19:30
March 23rd 20 at 19:23
Solution
try in the console to bring this.classList , without jquery
undefined - tamara_Barrows commented on March 23rd 20 at 19:26
@tamara_Barrows, I'm right here on the toaster tried in the console, everything works

$("code").each(function(e) { 
$(this).hover(function () { 
console.log(this.classList);
})
});
- ceasar.Ko commented on March 23rd 20 at 19:29
@ceasar.Ko,
I'm right here on the toaster tried in the console, everything works

Brilliant! - stella14 commented on March 23rd 20 at 19:32

Find more questions by tags jQueryJavaScript