How to focus text field after using extjs Ext.MessageBox.alert

I want to test my extjs form. When the text field is empty, a warning window should be displayed, after which our cursor should focus on the text field. I tried with the encoding below. Alert is working fine. but the cursor does not focus. Could you help me all concentrate?

if(Ext.get('first').dom.value=='') { Ext.MessageBox.alert('Status', 'Enter your First Name!'); document.getElementById("first").focus(); } 
+4
source share
3 answers

The documentation states that MessageBox is asynchronous:

Note that MessageBox is asynchronous. Unlike regular JavaScript Warning (which will stop browser execution), showing a MessageBox will not cause a stop. For this reason, if you have code that should run only after some users from MessageBox, you should use the callback function (see the function parameter for showing for more details).

So, your focus () is executed immediately after calling Ext.MessageBox.alert, and not when the user rejects it: when the user clicks to cancel the warning, the focus will change again.

Instead, you can try using regular javascript alert (), which is synchronous.

+8
source
 if (Ext.get("first").dom.value == "") { Ext.MessageBox.alert("Status", "Enter your First Name!", function() { Ext.get("first").focus(); }); } 
+6
source

try it

 if(Ext.get('first').dom.value=='') { Ext.Msg.alert('Status', 'Enter your First Name!', function(btn, text){ if (btn == 'ok'){ Ext.getCmp('first').focus(true,10); } }); } 
+1
source

All Articles