Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jquery focus out event

Tags:

jquery

I've a textbox having focusout event on it. whenever the textbox loses focus (Tab, mouse click on any other part of the form),based on certain conditions a user-written function disaplying an alert is called.

Now, my form also has a button, after filling the textbox when i click the button focusOut event of textbox is called instead of the button click event.

how do i stop the foucusout event of the textbox being called on button click ?

 ("$textbox").focusout(function(){
validation;
alert("message");
});

("$btn").click(function(){
----
});

can any one help me?

like image 754
jack_jill Avatar asked Aug 01 '11 09:08

jack_jill


1 Answers

I've changed your click event to a mousedown (so it fires first) and used a variable to track your click :

var buttonClicked = false;

$('#txt1').focusout(function(){
    if(!(buttonClicked)){
        alert('BlurEvent');
    }else{
        buttonClicked = false;
    }
});

$('#btn').mousedown(function(event){
    alert('ClickEvent');
    buttonClicked = true;
});

http://jsfiddle.net/924SZ/

EDIT

var buttonClicked = false;

$('#txt1').focusout(function(){
    if(!(buttonClicked)){
        // do normal focusout code.
    }else{
        // button was clicked - dont execute some code.
        buttonClicked = false;
    }
    //this always executes regardless of focusout or button click
    var reply =confirm("Are you happy with the input?");
});

$('#btn').mousedown(function(event){

    buttonClicked = true;
});
like image 123
cillierscharl Avatar answered Oct 26 '22 06:10

cillierscharl