JavaScript
Article
By Sam Deering

jQuery get element with current focus

By Sam Deering

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

--ADVERTISEMENT--

//get active element of focus using js
$(document.activeElement)
//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 */
         $(this).removeClass('item-changed');
      }
  });
});
Recommended
Sponsors
Get the latest in JavaScript, once a week, for free.