0

JQuery .hover() method will encounter problem when you want to run a loop on it. Say I have code as follow

<div class="screen screen_1"></div>
<div class="screen screen_2"></div>
<div class="screen screen_3"></div>
for(j=0; j<$('.screen').length+1; j++){
  $('.screen_'+ j +'').hover(
      function mousein(){
        //do something when mouse enters

      }, function mouseout(){
        //do something else when mouse leaves
      }
  );
}

now jhint is telling me not to do a function inside a loop so I have the following but it still doesn't like it.

var mousein = function(){
  //do something when mouse enters
};

var mouseout = function(){
  //do something else when mouse leaves
};

for(j=0; j<$('.screen').length+1; j++){
  $('.screen_'+ j +'').hover(
    mousein, mouseout;
  );
}

Thanks in advance

1
  • sorry guys...wasn't thinking straight. Thanks again for the help. Commented Jun 1, 2017 at 1:03

5 Answers 5

2

no need for the loop...

 $('.screen').hover(
    function(){
        //do something when mouse enters
        // $(this) gets you the element being affected.
      }, function(){
        //do something else when mouse leaves
      }
  );
Sign up to request clarification or add additional context in comments.

Comments

1

Remove the ; inside function arguments

$('.screen_'+ j +'').hover( mousein, mouseout/*;*/);
                                             //^^ not valid

Comments

0

Eliminates the need for external functions...

$('.screen').mouseenter(function() {
    //do something when mouse enters
}).mouseleave(function() {
    //do something else when mouse leaves
});

Comments

0

It's not necessary to loop over it to assign a handler. just use:

$(".screen").on("mouseenter mouseleave",function(event){
    switch(event.type){
       case "mouseenter":
           // code for mouse in
           break;
       case "mouseleave":
           // code for mouse out
           break;
}

use $(this) to use the element that triggered the event. for example if .screen_1 triggered the event $(this) will be essentially equal to using $(".screen_1")

3 Comments

There is no need to differentiate between .hover() and .mouseenter() when there's also a .mouseleave() event in play.
That didn't occur to me while I was writing it :P Thanks, I'll edit it
hover() does all that switch internally... no need to define individual events in a switch ... just adds code bloat for no reason
0

the problem with your loop is that you are essentially trying to loop for the first n frames. If you want something to loop when you hover over something then the loop needs to be inside of the function, not the function inside the loop. So your code should look a bit more like this:

$('.screen_'+ j +'').hover(
  function mousein(){
    //do loop

  }, function mouseout(){
    //do loop
  }
);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.