Monday, December 16, 2013

Setting the context in an jQuery ajax call

When an jQuery ajax call is made, the .done method has a different value of $(this)



Issue: $(this) is initially the control that called the event. After the ajax call, $(this) is different and attributes from the original value of $(this) before the ajax call is needed.

Solution: Set the context when setting to make the ajax call.

$.ajax({
          url: "/options/get?type=" + val,
          context: $(this)[0]
   }).done(function (d) { . . . } );


Source: http://api.jquery.com/jQuery.ajax

No comments:

Post a Comment