AJAX Closures and targeting 'this'

Posted by Nick Lowman on Stack Overflow See other posts from Stack Overflow or by Nick Lowman
Published on 2010-06-11T13:45:38Z Indexed on 2010/06/11 13:52 UTC
Read the original article Hit count: 126

Filed under:
|

In the code example below the success callback function logs 'input#04.update' four times rather than each individual input, which makes sense seeing how closures work but how would I go about targeting each individual input using this.

<input type="text" name="" id="01" class="update">
<input type="text" name="" id="02" class="update">
<input type="text" name="" id="03" class="update">
<input type="text" name="" id="04" class="update">

function updateFields(){
 $('input.update').each(function(){
    $this = $(this);
    $.ajax({
      data: 'id=' + this.id,
      success: function(resp){
       console.log($this);
          $this.val(resp)
      }
    });
  });
}

© Stack Overflow or respective owner

Related posts about JavaScript

Related posts about jQuery