Gabriel Muñumel
Gabriel Muñumel

Reputation: 1876

Set focus after an alert message in jquery

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

Answers (2)

stevemarvell
stevemarvell

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

RiderHood
RiderHood

Reputation: 79

Working code at JSFIDDLE. Just remove if(tofix).

Upvotes: 0

Related Questions