I have a text field that always needs to be focused no matter what the user does.
I have activated the text field on load by doing this:
<html>
<head>
< script type ="text/javascript" src ="//ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></ script>
< script type ="text/javascript">
$( function () {
$( "#myTextBox2" ).focus();
});
</ script>
</head>
< body>
< div>
< input type ="text" id ="myTextBox">
< input type ="text" id ="myTextBox2"onblur="var that= this; setTimeout(function() { that.focus(); }, 0);">
</ div>
</ body>
</html>
If I press the tab button when trying the code above I can still tab away from myTextBox2, but I cant click on myTextBox with my mouse (which is right).
If the user presses any button or clicks anywhere on the site the text field should still be selected and focused. How do I enforce this completely?
Edit:
Adding this to the header in my code and to the answer by Sven seems to do the trick:
$(document).keydown(function(objEvent) {
if (objEvent.keyCode == 9) { //tab pressed
objEvent.preventDefault(); // stops its action
}
})
Credit:
Lock tab key with javascript?
You can keep the focus on the text field at all time with the following code:
HTML:
JS:
Example on JsFiddle: Click!
I had to replace the value of the textfield to force/trigger the placement of the cursor in the focused field.
Hope this helps.