質問

I am trying for checking condition for textfield is focus or not

if($.txt_username.foucs() == 'true'){
alert('textfield username focus');

}
else{
alert('textfield username out of focus');
}

any one advice me how to check the condition for textfield is focus () or blur();

役に立ちましたか?

解決

add focus and blur events in your code to check when field is focussed and blurred. update boolian variable to set focus or blur state .Check that variable to perform any operation which you want to perform on focus or non focus (blur ) of textField.

 $.txt_username.addEventListener('focus', function() {
        focussed = true;
    });

    $.txt_username..addEventListener('blur', function() {
        focussed = false;
    });


if(focussed){
  //do whatever you want when field is focus
}else{
 //do whatever you want when field is not focus
}

他のヒント

focus is a jQuery function to set a focus handler function to the element. It doesnt test for whether the element is currently focused.

Example of use of focus:

$( "#target" ).focus(function() {
    alert( "Handler for .focus() called." );
});

Could you rewrite your logic so that you are notified when the element is focused? In the focus event handler you could write your code.

blur also works the same way. You can assign a blur handler function using the blur() function.

Testing for focus with JS:

var selected = document.activeElement;
if (selected.id == "myTxtIdUName") {
  alert('Field username focused');
}else{
  alert('Field username NOT focused');
}

Note: Active element of html page in a background window doesn't have focus. So, if you want to check for that also, you can use selected.hasFocus for more accurate results.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top