Not working for me.. can u help me?


#1

$('div').hover(
    function(){
      $(this).addClass('active');
   },
   function(){
      $(this).removeClass('active');
   }
);

#2

I'm guessing you're on this lesson - 6. Hover

A few mistakes:

  1. Where's your document .ready at the start ?
  2. This line is broken: function(){
  3. here :arrow_down:

you need to change }; to: }); this: ) is for closing the functions bracket
4.

You need to add mouseleave to 'div' and in the function add remove class. Like so:

$('div').mouseleave(function(){
$(this).removeClass('active');
});

*Remember in order for you to pass even if the code is correct you need to hover over the div wait a few seconds remove your mouse from the div wait a few seconds and then exit preview.

Have a look at below if you don't understand don't copy and paste


In conclusion:

$(document).ready(function(){

//below this is so when you hover the class is added
  $('div').hover(function(){
    $(this).addClass('active');
    });

//below this is so that when your mouse leaves the div the class gets removed
    $('div').mouseleave(function(){
     $(this).removeClass('active');
    });

}); //closing document.ready

#3

Thank u, if i copy your codes, i get like this.

Oops, try again.
It does't look like your background color changed, is your hover adding the class active?
.


#4

As I said understand and correct your mistakes don't copy, but my code is correct unless I'm missing something,

You need to test the code correctly by waiting a few seconds after hovering and a few seconds after you move your mouse out..

Can you see the changes in the div?


#5

ok.. copy your correct codings and send me for my verification.


#6

Hi Vijaya :slight_smile:

The only problem your code has is that it does not have $(document).ready, as has been told by @zainabrawat earlier. With that function put in its proper place, your code woks perfectly :smiley:

Because that function wasn't there, your function was running before those divs loaded, and so was trying to attach the hover handler to divs that didn't exist at that time. Hence, you couldn't see the effect of your hover handler.

Hope it helps! :smiley:


#7

A post was split to a new topic: 6. Hover


#8