I need assistance in fetching the class name by hovering over a div. Both divs have the same id but slightly different class names. Here is an example:
<div id="1-someid" class="1-example-class border cz">
...more elements go here....
</div>
and
<div id="2-someid" class="2-example-class border cz">
...more elements go here....
</div>
Update: I've updated the id names to be unique as suggested by experts below. :) Thank you for all the help.
Now, what I am looking for is that when a user hovers over the div with 1-example-class
, it should return the class name 1-example-class
. Similarly, when someone hovers over the div with 2-example-class
, it should return the name 2-example-class
.
This way, I can use parseInt()
on the name to retrieve the number 1, 2, 3, and so on.
It's important to note that writing a static script for just 1-example-class
or 2-example-class
won't suffice as there are many more divs like this with different numbers attached to them.
Can anyone assist me with this? I tried the following code snippet but it didn't work:
$('#someid').hover(function() {
var class_names = $(this).attr('class');
var class_name = class_names.split( ' ' );
var c = parseInt( class_name[0] );
console.log( c );
});
Your help would be greatly appreciated.