Pergunta

I'm relatively new to code and have been building a little project called http://keyboardcalculator.com/ to improve my jQuery.

I'm trying to add some more features to improve some things - and I've coded them! - but I want to add a button at the bottom of the page that displays a div to tell people what they are.

I don't want to tarnish the user flow when the button is clicked - and a big part of that for me is that I don't want the user to have to click back into one of the text-input boxes to keep doing calculations.

I want the focus to go back onto the last text-input that the user had focus on. This is my attempt to get it working on the #ansinput text-input field (the big one with the answer in):

if ($('#ansinput').is(':focus')) {
        $('#more-commands-button').click(function() {
            alert('hello')
        });
}

But I'm not getting my alert.

Any help as where to go from here would be fantastic!

Thank you very much StackOverflow!

Foi útil?

Solução

It seems like you want to track the last text input that had focus before the click. So you'd create a variable, store it on focus and then call focus on that in the button click.

var $lastfocus = $('input:text:eq(0)');  // initialize to first one.
$('input:text').focus(function() {
   $lastfocus = $(this);
});

$('#more-commands-button').click(function() {
   alert('hello');
   $lastfocus.focus();

 });
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top