1

I have this auto suggest menu with my input fields and as there are multiple fields I need to select the field that was in focus just before the li was clicked and blurred the input, so it knows which input to select. is there a way to get the last input field focused before the blur? Thanks

function fill(thisValue) {
 if(thisValue === undefined){
   $('#suggestions').fadeOut();
    } else {
    $("input *//input in focus before blur//* .val(thisValue);
    setTimeout("$('#suggestions').fadeOut();", 600);
    $('.email').addClass('load');

     }
  }
4

1 に答える 1

1

Yes there is, but you'll need to store the last focused element when the LI is clicked, or more accurately before it's clicked, and blur is too late.

var lastFocused = null;

$('li').on({
    mousedown: function() {
        lastFocused = document.activeElement; //saves focused element
    },
    click: function() {
        //do whatever you do on click
    }
});

function fill(thisValue) {
   if(thisValue == undefined && lastFocused) {
       $('#suggestions').fadeOut();
   } else {
       lastFocused.value = thisValue;
       setTimeout(function() {
          $('#suggestions').fadeOut();
       }, 600);
       $('.email').addClass('load');
  }
}

Here's a quick DEMONSTRATION to show that it works!

If you can avoid globals by using data() or something similar, that would be better, it's just to demonstrate how it's done.

于 2013-02-17T01:23:29.747 に答える