jQuery get element with current focus

Sam Deering

jQuery code snippet to get element with current focus and perform AJAX auto save once it looses focus.

//get active element of focus using js
//change focus when tabbed across into new item
$('.item :input').live('focus', function()
  var thisItem = $(this).parents('.item');
  var thisId = thisItem.attr('id');
  $('.item').removeClass('item-selected'); //remove all focus
  thisItem.addClass('item-selected');  //add focus to current item

  //save those items that have changed
  $.each($('.item-changed'), function(i,v)
      var currItemId = $(this).attr('id');
      //exclude current item
      if (!_this.helpers.itemHasFocus(currItemId))
         console.log('saving '+currItemId);
         _this.save.item(currItemId); /* AJAX auto save */