I want to protect an input by a confirmation popup window, and when one confirms, i want to delete this protection, and give the focus to the input. My attempt was:
my_input.on('focus', switch_off_focus_handler(my_input));
function switch_off_focus_handler(input){
var confirm = confirm("Are you sure?");
if (confirm) {
input.off('focus');
input.focus();
}
}
When I click on the input, I get the confirmation popup. If I click OK, the on_focus handler is indeed deleted (because clicking a second time on the input will not trigger the popup), but the input does not get the focus. (input.focus() seems to have no effect).
Am I doing wrong ?
EDIT: Sorry, that was not the correct code i used. Here it is (With the html environment for testing):
<!doctype html>
<html>
<head>
<script src="/static/js/jquery-1.11.1.min.js" type="text/javascript"></script>
</head>
<body>
<input type="text" value="HALLO" id="my_input">
<script type="text/javascript">
my_input=$("input#my_input")
my_input.on('focus', switch_off_focus_handler(my_input));
function switch_off_focus_handler(input){
return function () {
var conf = confirm("Are you sure?");
if (conf) {
input.off('focus');
input.focus();
}
}
}
</script>
</body>
</html>
The problem seems to come from the confirm dialog. If i replace
var conf = confirm("Are you sure?");
by
var conf = true;
then it works.