1

I am trying to find some javascript that will keep an input focused when the webpage is not the active page.

For example, I have 2 firefox windows open, one is for reading and the other has some simple counters (press the one of the input buttons and it counts).

The annoying issue is having to click back and forth between the windows (you have to have the counter window active before you can click the button). What I am hoping will work is making the last button pressed stay focused even when the page is not active.

This is basically what I have been trying with jquery:

<html>
<head>
<script src="http://jqueryui.com/jquery-1.4.4.js"></script> 
<script type="text/javascript">
$("input.focus:last").focus();  
</script>
</head>
<body>
<input type="button" name="counter1" size="10" class="focus" value="button1">
<input type="button" name="counter2" size="10" class="focus" value="button2">
</body>
</html>
1
  • "you have to have the counter window active before you can click the button" > do you ? I can click directly on FF / Ubuntu, the first click will set the window as active and the onclick action will be triggered.. Commented Feb 28, 2013 at 13:50

2 Answers 2

1

Wrap your code in a DOMReady handler:

$(function() {
   $(window).bind('blur', function() {
     $("input.focus:last").focus();  
   }).trigger('blur'); //run straight away
});

jsFiddle example

Sign up to request clarification or add additional context in comments.

Comments

0
$(document).ready(function(){

  var button = $("input.focus:last");

  button.blur(function(){
    try {$(this).focus();}
    catch(Error){}
  });

  button.focus();
});

add try catch too. so when your input object is not visible, they dont get error

2 Comments

neither option seems to be making the last button always active. Is there something more I am missing other than plugging the above code into the script?
meh, still not. Maybe its not possible

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.