1

My question is. I've a barcode-scanner on my website in order to verify if a student is in classroom. The INPUT element of my form must always have focus in order to recieve the code from the barcode-scanner.

My code is:

    $("#codebarre").focus()
    $("body").click(function(){
        $("#codebarre").focus()      
})

The second entry in order to recover the focus if i click on the page. My problem is: i have the possibility to write a comment in another input in the page. With my code, it's true that just after a click in my input, #codebar recover focus and i'm not able to write in my input. So my need is: always focus in #codebar except if i click (focus) in my remark INPUT.

I try this:

    $("#codebarre").focus()
    $("body").click(function(){
    if( !$(".remark").focus() ){
        $("#codebarre").focus()  
    }        
})

My code don't work. Can you tell me what I'm doing wrong ?

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
Hugo
  • 494
  • 1
  • 5
  • 18

4 Answers4

3

You can't determine if a control has the focus in that manner. focus() will only set the focus to that control. See this answer for a detailed explanation of how to determine if a control has the focus: Using jQuery to test if an input has focus

If you are using jQuery 1.6, you should be able to do:

$("body").click(function(){
    if (!$(".remark").is(":focus")) {
        $("#codebarre").focus()  
    }

EDIT
If you are using an earlier version of jQuery, you can use $(document.activeElement) to determine which control has the focus. In that case, the code should be:

$("body").click(function(){
    if (!$(document.activeElement).hasClass("remark")) {
        $("#codebarre").focus()  
    }
Community
  • 1
  • 1
rsbarro
  • 27,021
  • 9
  • 71
  • 75
  • Sorry, i forgot to give my jquery version ;( I'm using 1.4.2. I must copy my website in order to have a dev version and will test with 1.6. I give a feedback ASAP. Thanks for response. – Hugo May 30 '11 at 18:39
1

Use document instead of body and change !$(".remark").focus() to $(".remark:focus").length == 0 or !$(".remark").is(":focus").

$(document).click(function(){
    if($(".remark:focus").length == 0) {
        $("#codebarre").focus() ;
    }        
})

See fiddle.

melhosseiny
  • 9,992
  • 6
  • 31
  • 48
  • Thanks, your code work for me, but only after adding this piece of code: $("#codebarre").val(''); on third line I will check tomorrow at work with the barcode-scanner before close my question. – Hugo May 30 '11 at 18:32
0

Use the :focus selector. http://api.jquery.com/focus-selector/

if($(".remark:focus").length==0 ){
        $("#codebarre").focus()  
} 
Niklas
  • 29,752
  • 5
  • 50
  • 71
0

You could use the OnFocus event for the comment box to run a piece of code that sets a variable. So that when the input is set to the comment box, the variable is set to false. And if the variable is set to false, you need to make sure that it doesn't change the focus to the barcode box anymore.

RobinJ
  • 5,022
  • 7
  • 32
  • 61