Reputation: 1876
I'm trying to set the focus in a input after an alert box appear. I have problem to set the focus on my field, ref.
My code is simple:
Javascript
var toFix = true;
$( "#add" ).click(function(e) {
if(toFix){
if (confirm('Not valid')) $( "#ref" ).focus();
e.preventDefault();
}
});
HTML:
<input class="input-text submit" type="submit" id="add" name="submit" value="Add" style="text-align: center"/>
<input class="input-text" type="text" name="ref" id="ref">
EDIT: Demo
Upvotes: 3
Views: 7552
Reputation: 993
Don't use a confirm
:
var toFix = true;
$( "#add" ).click(function(e) {
if(toFix){
alert("fix it");
$( "#ref" ).focus();
e.preventDefault();
}
});
http://jsfiddle.net/stevemarvell/fEmfa/
Upvotes: 1