Umren
Umren

Reputation: 392

hover and if statement firing only ONCE

mouseover should be bound as event that checks "if" statement each time mouse enters a region of .input-medium, but "if" checks only 1 time. Of there would be no class and "if" becomes false - hover will still be working.

I need to check if class exists each time.

$('.input-medium').on('mouseover' , function() {
    if ($(this).attr('id') === 'error-highlight') {
           $(this).hover(
                  function() {
                      $('<p class="reg-tooltip">test test</p>').appendTo('body');
                  },

                  function() {
                      $('p').remove();
                  });
       }
});

Upvotes: 0

Views: 753

Answers (1)

Rob W
Rob W

Reputation: 348992

Solution: Add the if condition to hover itself:

$('.input-medium').hover(
     function() {
         if (this.id === 'error-highlight') {
             $('<p class="reg-tooltip">test test</p>').appendTo('body');
         }
     },
     function() {
         $('p.reg-tooltip').remove(); // <-- Always remove temporary element
     }
);

Upvotes: 3

Related Questions