Adrian M.
Adrian M.

Reputation: 7423

jQuery prevent text field from losing focus unless user clicks outside

I have a template with a full screen slide show that makes a text field lose focus on mobile devices.. I want to know if there is a fix that can prevent the field lose focus unless the user clicks outside the field..

I have browsed the other questions but found nothing close to what I need.. I appreciate any reply.. Thank You..

Upvotes: 1

Views: 1264

Answers (1)

faby
faby

Reputation: 7558

are you searching something like this? this function gives focus to your input after every click on the body

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

update with correct solution

working fiddle http://jsfiddle.net/JJLN8/2/

check if the element with focus is an input, if yes change focus else remain in the last input selected. click in the red area that is the body to see that the focus remain in the input

var lstfocus=$("input")[0];
$("body").click(function(){
    if ($(':focus').is("input")    ){
          lstfocus= $(':focus');
    }
    lstfocus.focus();    
});

css

div{width:200px;height:200px; background-color:red}

html

<div>
    <input type="text"  /><br/>
    <input type="text" /><br/>
 </div>

Upvotes: 2

Related Questions