jQuery Hover does not work

0

I have <button> on the page that after an action receives a new class, in this new class I need to hover the text to change, but it does not work, the new class does not seem to be recognized, can someone please help me?

After an action the button class changes from .see-move to .buy:

$('.see-movie').removeClass().addClass('buy').text('Gostou?Adquira já!');

In this new class when using the jQuery hover () feature the text needs to change:

$('.buy').hover(function(){
  $(this).text('Ligue 0800 123 4567')
  }, function(){
  $(this).text('Gostou?Adquira já!')
});
    
asked by anonymous 29.06.2017 / 18:03

4 answers

6

You do not need Javascript .

Nowadays, a lot of the hover and toggle issues can only be handled with HTML and CSS. I would say Javascript should be used more to handle some behavior that should be dynamic. In your case, you have a text and want that when the :hover event occurs, this text will be changed.

You can use data attributes ( those attributes that begin with data-* ) to define which text should be displayed with and without hover. For example:

<!-- TODO: usar nomes melhores para o atributo? -->
<a href='#' data-text-with-hover='Gostou? Adquira já!'
            data-text-without-hover='Ligue 0800 123 4567'>
</a>

With this, in CSS you can use the function attr() of the CSS to get the attributes of the element, in the case data-text-with-hover and data-text-without-hover . Thus, you can change the content of the element according to its current state. For example:

elemento::after { 
   content: attr(data-text-without-hover)
}

elemento:hover::after {
   content: attr(data-text-with-hover)
}

Follow a snippet:

a {
  background: url(http://lorempixel.com/400/200/technics);
  display: inline-block;
  position: relative;
  height: 200px;
  width: 400px;
}

a::after {
  background: rgba(0, 0, 0, .8);
  bottom: 0;
  color: #fff;
  content: attr(data-text-without-hover);
  display: inline-block;
  height: 40px;
  line-height: 40px;
  position: absolute;
  left: 0;
  text-align: center;
  width: 100%
}

a:hover::after {
  content: attr(data-text-with-hover);
  color: gold
}
<a href='#' data-text-with-hover='Gostou? Adquira já!'
            data-text-without-hover='Ligue 0800 123 4567'></a>
    
29.06.2017 / 18:50
0

Try this:

$('.see-movie').toggleClass('buy').text('Gostou?Adquira já!');
$('.buy').hover(function() {
    $(this).text('Ligue 0800 123 4567');
}, function() {
    $(this).text('Gostou?Adquira já!');
});
    
29.06.2017 / 18:25
0

Try using the following:

$(document).on('hover','.buy', function(){
  $(this).text('Ligue 0800 123 4567');
}, function(){
  $(this).text('Gostou?Adquira já!');
});
    
29.06.2017 / 18:31
0

Capture mouse events

Instead of hover use mouse events at the document level:

$(document).on("mouseenter", ".buy", function() {   
  $(this).text('Ligue 0800 123 4567');
});

$(document).on("mouseleave", ".buy", function() {
  $(this).text('Gostou?Adquira já!');
});

JSFiddle with an example

    
29.06.2017 / 18:58