I've been breaking my head over the following issue now and hope you guys can point me to the (probably obvious) solution.
I have a simple web application that takes input a input field (type="text"). When the page loads, the cursor is ready to accept input. As soon as the user navigates away (onBlur) the AJAX part kicks in, I look up the code in the database and depending on the result the box on the right goes green or stays red.
After that change, I want the input field cleared and ready to receive new input. Clearing the field does work, setting focus to it does not however. I tried focus() and select(), no go... I tried to fold in a setTimeout, no go. With the current code it doesn't work in FF, chrome and IE. I hope you guys can give me a hint.
This is the webpage code:
<!DOCTYPE HTML>
<html>
<head>
<script type="text/javascript">
function checkTicket(tno) {
var xmlhttp;
if (tno=='') {
document.getElementById('inside').innerHTML='0';
document.getElementById('validbox').style.backgroundColor='#FF0000';
return;
}
if (window.XMLHttpRequest) {
// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
} else {
// code for IE6, IE5
xmlhttp=new ActiveXObject('Microsoft.XMLHTTP');
}
xmlhttp.onreadystatechange=function() {
if (xmlhttp.readyState==4 && xmlhttp.status==200) {
document.getElementById('inside').innerHTML=xmlhttp.responseText;
if (document.getElementById('inside').innerHTML=='1') {
document.getElementById('validbox').style.backgroundColor='#00FF00';
} else {
document.getElementById('validbox').style.backgroundColor='#FF0000';
}
set_focus();
}
}
xmlhttp.open("GET","check_eticket.php?t="+tno,true);
xmlhttp.send();
}
function set_focus() {
document.form.ticketnumber.value='';
document.form.ticketnumber.focus();
}
</script>
</head>
<body onLoad="set_focus()">
<div id="wrapper" style="position: absolute; top: 50%; height: 400px; margin-top: -200px; width: 99%;">
<div id="innerwrap" style="width: 75%; margin-left: auto; margin-right: auto;">
<div style="float: left; width: 50%; margin-left: auto; height: 400px; padding-top: 115px;">
<span style="font-size: 3em; font-weight: bold; text-align: center;">TOEGANGSCODE:<br /></span>
<form name="form">
<input name="ticketnumber" id="ticketnumber" type="text" onBlur="checkTicket(this.value)" size="11" style="font-size: 3.55em; font-weight: bold;" />
</form>
</div>
<div id="validbox" style="float: right; width: 50%; background-color: #FF0000; height: 400px; margin-right: auto;">
<div id="inside" style="display: none;">0</div>
</div>
</div>
</div>
</body>
</html>