Zeki
Zeki

Reputation: 73

Working with multiple class

I have two nested divs.

<div class="AKM _ana_div">  <div class="AKM  Pazartesi 0830 1229"></div>  </div>

I want to trigger a function when mouse enters second div.

I tried this but apparently its not working

    $(".AKM").mouseenter(function() {
       if ($(this).attr('class') == "0830") { 
            if ($(this).attr('class') == "Pazartesi") {
               DO THIS
            }
       }
   });

What I am missing here?

Upvotes: 1

Views: 30

Answers (2)

Jai
Jai

Reputation: 74738

Try this:

$(".AKM").mouseenter(function(e) {
   e.stopPropagation();  // <---------add this to stop event bubbling
   if ($(this).hasClass("0830") && $(this).hasClass("Pazartesi")) {
        //   DO THIS
   }
});

Because .attr() method returns you the class names in your code context and you are checking for just one class, so its always gets failed. Instead you have to use .hasClass() which returns a boolean if something you are looking for is found.

Upvotes: 1

techfoobar
techfoobar

Reputation: 66663

You can attach the mouseenter event handler directly to the element(s) you want:

// match element(s) with classes AKM, 0830 and Pazartesi
$(".AKM.0830.Pazartesi").mouseenter(function() {
    ...
});

Note: You can do this in a cleaner way using data attributes and using the class attribute for just styling.

Upvotes: 0

Related Questions