Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I reference the calling dom element in a jquery ajax success function?

I'm trying to modify the class of an element if an ajax call based on that element is successful

<script type='text/javascript'>
$("#a.toggle").click(function(e){
    $.ajax({
        url: '/changeItem.php',
        dataType: 'json',
        type: 'POST',
        success: function(data,text){
            if(data.error=='')
            {
                if($(this).hasClass('class1'))
                {
                    $(this).removeClass('class1');
                    $(this).addClass('class2');
                }
                else if($(this).hasClass('class2'))
                {
                    $(this).removeClass('class2');
                    $(this).addClass('class1');
                }
            }
            else(alert(data.error));
        }   
    });
    return false;
});
</script>
<a class="toggle class1" title='toggle-this'>Item</a>

My understanding of the problem is that in the success function this references the ajax object parameters, NOT the calling dom element like it does within other places of the click function. So, how do I reference the calling dom element and check / add / remove classes?

like image 324
Issac Kelly Avatar asked Sep 30 '08 16:09

Issac Kelly


People also ask

How do you call a function after a success on AJAX?

Call function after ajax complete You can simply copy/paste code in the body tag of the html page and reload page to get the content form https://api.joind.in/v2.1/talks/10889 URL after success and call callFunctionAfterAjaxCallComplete() function after ajax request is completed.

How do you call a controller in a success with AJAX?

So, add a success option to your partial-view ajax request and then get the response and render it in the div you want the partial-view to be visible as: success: function(response){if(response!= null) $('<yourDivInWhich you want to render the partial view>'). html(response)} .

What is the AJAX calling function in jQuery?

The ajax() method is used to perform an AJAX (asynchronous HTTP) request. All jQuery AJAX methods use the ajax() method. This method is mostly used for requests where the other methods cannot be used.

Which method is used on the returned object of AJAX () method if the AJAX call fails?

If an AJAX request fails, you can react to the failure inside the callback function added via the fail() function of the object returned by the $. ajax() function. Here is a jQuery AJAX error handling example: var jqxhr = $.


2 Answers

You can just store it in a variable. Example:

$("#a.toggle").click(function(e)
{
   var target = $(this);
   $.ajax({
      url: '/changeItem.php',
      dataType: 'json',
      type: 'POST',
      success: function(data,text)
      {
         if(data.error=='')
         {
            if(target.hasClass('class1'))
            {
               target
                  .removeClass('class1')
                  .addClass('class2');
            }
            else if(target.hasClass('class2'))
            {
               target
                  .removeClass('class2')
                  .addClass('class1');
            }
         }
         else(alert(data.error));
      }       
   });
   return false;
});
like image 85
Dan Goldstein Avatar answered Nov 16 '22 00:11

Dan Goldstein


jQuery passes the target of the event, along with some other information about it, to your handler function. See http://docs.jquery.com/Events_%28Guide%29 for more info about this.

In your code, it'd be referenced like $(e.target).

like image 45
Ellen Teapot Avatar answered Nov 16 '22 00:11

Ellen Teapot