Question

The HTML is a list of buttons, that toggles an element on an off.

I want to change the text on the button, from "show" to "hide". i want it to be done from withing the complete() callback. how can i access the element that was clicked?

I have this JS-code:

  $("body").on("click", ".selector", function(eve) {
     $(".selector", $(this).parent()).toggle(function(){
        $(/*here comes the element that was clicked*/).text(
            function(i,text){
                return text == "show" ? "hide" : "show"; 
            }
        );
     });
  });
Was it helpful?

Solution

Store a reference to this

$("body").on("click", ".selector", function(eve) {
 var that = $(this);
 $(".selector", $(this).parent()).toggle(function(){
    that.text(
        function(i,text){
            return text == "show" ? "hide" : "show"; 
        }
    );
 });
});

OTHER TIPS

You can use the event's event.currentTarget property

$("body").on("click", ".selector", function (eve) {
    $(this).parent().find(".selector").toggle(function () {
        $(eve.currentTarget).text(function (i, text) {
            return text == "show" ? "hide" : "show";
        });
    });
});
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top