funbinod Posted June 11, 2015 Share Posted June 11, 2015 hi guys! greetings! I've used ajax to get some data after user inputs some information on a <input> and hits enter. function getAid(){var XMLHttpRequestObject = false;if (window.XMLHttpRequest) { XMLHttpRequestObject = new XMLHttpRequest();} else if (window.ActiveXObject) { XMLHttpRequestObject = new ActiveXObject("Microsoft.XMLHttp");}XMLHttpRequestObject.onreadystatechange = function(){ if (XMLHttpRequestObject.readyState == 4 && XMLHttpRequestObject.status == 200) { document.getElementById('aid').value = XMLHttpRequestObject.responseText; }}var name = encodeURIComponent(document.getElementById('name').value);XMLHttpRequestObject.open('POST', 'ajax/aid.php');XMLHttpRequestObject.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");XMLHttpRequestObject.send("name=" + name);} this function is set onBlur() method on #name field. its working fine. but I wish something more. I wish that the cursor should move to another field only after the successful ajax callback, or say only after getting data on #aid field. for example <input id="name"> <input id="address"> <input id="aid"> when user inputs 'name' and hits 'enter', value is derived from ajax request to 'aid' field and after getting the value, the 'address' field with get the focus.. please guide through... thanks in advance.... Link to comment Share on other sites More sharing options...
dsonesuk Posted June 11, 2015 Share Posted June 11, 2015 Check if returned value is valid if it is then after document.getElementById('aid').value = XMLHttpRequestObject.responseText; set focus on address input. Link to comment Share on other sites More sharing options...
justsomeguy Posted June 11, 2015 Share Posted June 11, 2015 It might be a little confusing, but you could make the field disabled until the response comes back. Link to comment Share on other sites More sharing options...
funbinod Posted June 12, 2015 Author Share Posted June 12, 2015 ok I've done this $('#name').keyup(function(e) { getAid(); // this is the function for ajax request $('#address').attr('disabled', 'disabled'); if (e.which === 13) { if( ($('#aid').val() !== '')) { e.preventDefault(); $('#address').removeAttr('disabled'); $('#address').focus(); } else { alert('Error!'); $('#name').select(); } } }); please guide me where I've done mistake. everything seems to be working fine but the focus goes to #address whether the ajax is successful or not... Link to comment Share on other sites More sharing options...
justsomeguy Posted June 12, 2015 Share Posted June 12, 2015 The ajax request is asynchronous, you need to check for the return value and enable the field in the ajax callback once the request finishes. Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now