Parameters functions for click event do not work

Asked

Viewed 95 times

1

I’m trying to highlight a text with the event click and then "slip" on the second click, I took the hint of a code that I had already seen on codepen , I am passing two functions to the event click, but only the second works, code:

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

        alert(1);

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

Only the Alert(2), why does this happen?

  • But the function click accepts only one function Handler at a time. If two parameters are passed, the first one will be eventData and only the second will be the handler See the documentation. What exactly do you intend to do?

  • I want to highlight the text, would use the css() function, with text-Decoration, take a look at the example I put of codepen that uses this concept, or I left something unnoticed rs

  • Yes. In the example it uses the function hover which in fact has two functions, handlerIn and handlerOut. The click doesn’t work that way.

  • Ah, I see, what would be the best way to do that?

  • Will be the element .texto integer that will be marked or some text that is selected?

  • The same text element.

  • 1

    It would look something like this: https://jsfiddle.net/acwoss/1j9qcfs1/ ?

  • That’s right, thank you!

  • Is there an event like this toggle? because I also want to use ajax, to change the status, it would be something like 0 and 1, only dynamically.

  • How so event like toggle?

  • Toggle between clicks, for example, the application would be a "list of things to do", on the first click, text is underlined, with ajax I change the status to 1, and on the second click, it would be "unlinked" and ajax would change the status to 0...

Show 7 more comments

1 answer

3


From what you described, just use the function toggleClass jQuery to add/remove a CSS class from the pressed element. When an element is pressed, jQuery will check if it has the CSS class; if it does not have it, add it; if it has it, 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>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.