focus();

I can not get two functions to work at once.
I try to get information with ajax and put it in a div, and focus the input box, but it will not focus the input box.
I need both actions to happen.
<html>
<head>
<link href="../../include/chat.css" rel="stylesheet" type="text/css" />
<script language="javascript" type="text/javascript">
<!--
function doall(){
doWork();
focusit();
}
function focusit(){
document.chathtmlform.inputText.focus()
}
// Get the HTTP Object
function getHTTPObject(){
if (window.ActiveXObject) return new ActiveXObject("Microsoft.XMLHTTP");
else if (window.XMLHttpRequest) return new XMLHttpRequest();
else {
alert("Your browser does not support AJAX.");
return null;
}
}
// Change the value of the outputText field
function setOutput(){
if(httpObject.readyState == 4){
document.getElementById('outputText').innerHTML += httpObject.responseText;
}
}
// Implement business logic
function doWork(){
httpObject = getHTTPObject();
if (httpObject != null) {
httpObject.open("GET", "../../chat.php?inputText="
+document.getElementById('inputText').value, true);
httpObject.send(null);
httpObject.onreadystatechange = setOutput;
}
}
var httpObject = null;
 
 
//check see if enter was pressed.
function checkEnter(e){ //e is event object passed from function invocation
var characterCode
 
if(e && e.which){ //if which property of event object is supported (NN4)
e = e
characterCode = e.which //character code is contained in NN4's which property
}
else{
e = event
characterCode = e.keyCode //character code is contained in IE's keyCode property
}
 
if(characterCode == 13){ //if generated character code is equal to ascii 13 (if enter key)
doall();
}
 
}
//-->
</script>
</head>
<body>
 
<div id="outputText">
Welcome to Gates of Anrah.<br/>
Play fair and have fun!
<div id="chatform">
<form name="chathtmlform" id="chathtmlform" onSubmit='return false;'>
<input type="text" name="inputText" id="inputText" onKeyUp='checkEnter(event)' autocomplete="off"/>
<input type="button" onclick="doall()" value="Chat"/><br/>
<a href="../../chat.php?log=logout">Log Out</a>
</div>
</div>
</body>
</html>

Open in new window

LVL 2
Unspoken1Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

hieloCommented:
try setting the focus AFTER you get the response and allow for a small delay. Save this as test.html and try it.
<html>
<head>
<link href="../../include/chat.css" rel="stylesheet" type="text/css" />
<script language="javascript" type="text/javascript">
<!--
function doall(){
	doWork();
}
function focusit(){
	document.getElementById("inputText").focus();
}
// Get the HTTP Object
function getHTTPObject(){
	if (window.ActiveXObject) return new ActiveXObject("Microsoft.XMLHTTP");
	else if (window.XMLHttpRequest) return new XMLHttpRequest();
	else {
		alert("Your browser does not support AJAX.");
		return null;
	}
}
// Change the value of the outputText field
function setOutput(){
	if(httpObject.readyState == 4){
		document.getElementById('outputText').innerHTML += httpObject.responseText;
		setTimeout("focusit()",100);
	}
}
// Implement business logic
function doWork(){
	httpObject = getHTTPObject();
	if (httpObject != null) {
		httpObject.open("GET", "../../chat.php?inputText="+document.getElementById('inputText').value, true);
		httpObject.send(null);
		httpObject.onreadystatechange = setOutput;
	}
}
var httpObject = null;
 
 
//check see if enter was pressed.
function checkEnter(e){ //e is event object passed from function invocation
	var characterCode
 
	if(e && e.which){ //if which property of event object is supported (NN4)
		e = e
		characterCode = e.which //character code is contained in NN4's which property
	}
	else{
		e = event
	characterCode = e.keyCode //character code is contained in IE's keyCode property
	}
 
	if(characterCode == 13){ //if generated character code is equal to ascii 13 (if enter key)
		doall();
	} 
}
//-->
</script>
</head>
<body>
 
<div id="outputText">
Welcome to Gates of Anrah.<br/>
Play fair and have fun!
<div id="chatform">
<form name="chathtmlform" id="chathtmlform" onsubmit='return false;'>
<input type="text" name="inputText" id="inputText" onKeyUp='checkEnter(event)' autocomplete="off"/>
<input type="button" onclick="doall()" value="Chat"/><br/>
<a href="../../chat.php?log=logout">Log Out</a>
</div>
</div>
</body>
</html>

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Unspoken1Author Commented:
Thanks, that was perfect, I wonder why you have to have a timeout between those two function calls?
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
AJAX

From novice to tech pro — start learning today.