Parameter functions for click event do not work

1

I'm trying to rename a text with the click event and then "swoop" in the second click, I got the hint from a code I'd already seen on codepen , I'm passing two functions to the click event, but only the second one works, code:

$('.texto').click(function() {

        alert(1);

    }, function() {
        alert(2);
    }
);

Only alert (2) appears, why does this happen?

    
asked by anonymous 22.07.2017 / 01:55

1 answer

3

For what you've described, just use the toggleClass function of jQuery to add / remove a CSS class from the element you've pressed. When an element is pressed, jQuery will check if it has the CSS class; if it does not have, the addition; if you have, remove it.

$(() => {

  // Evento 'click' dos elementos desejados:
  $("li").on("click", function (event) {

    // Adiciona/remove a classe CSS:
    $(this).toggleClass("selected");

  });

});
.selected {
  font-weight: bold;
  background: cyan;
}
<ul>
  <li>Abacaxi</li>
  <li>Banana</li>
  <li>Caqui</li>
  <li>Damasco</li>
</ul>
    
22.07.2017 / 02:30