By Sam Deering

jQuery get element with current focus

By Sam Deering
Help us help you! You'll get a... FREE 6-Month Subscription to SitePoint Premium Plus you'll go in the draw to WIN a new Macbook SitePoint 2017 Survey Yes, let's Do this It only takes 5 min

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);; /* AJAX auto save */
Login or Create Account to Comment
Login Create Account
Get the most important and interesting stories in tech. Straight to your inbox, daily.Is it good?