Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Data input how to

Status
Not open for further replies.

jjvb

Programmer
Mar 4, 2007
2
US
I am not a programmer but am messing around with html/javascript to put together some small routines to help my kids with math facts. I can't figure out how to have a data entry box on the screen with the cursor, where a number can be typed in and a function executed upon pressing the enter key. The cursor needs to return to the data entry area after each execution of the function. Thanks.
 
Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "[URL unfurl="true"]http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">[/URL]
<html xmlns="[URL unfurl="true"]http://www.w3.org/1999/xhtml">[/URL]

<head>
<script type="text/javascript" language="javascript">
function runFunction(e){

	var keyPressed = (window.event ? event.keyCode : e.which); // Set up values so that we use the right
	var srcObject = (window.event ? event.srcElement : e.target); // terminology for IE and FireFox
	if(keyPressed == 13){ //Enter Key
 		var value = parseInt(srcObject.value);
 		if(!isNaN(value)){ //Making sure a number was entered
 			// Perform some Math function on the value
 			alert("The square root of " + value + " is " + Math.sqrt(value));
 		}
 		else {
 			alert("I'm sorry but '" + srcObject.value + "' is not a number in this universe.");
 		}

	 	//Clear the input field
	 	srcObject.value = "";
 	
	 	//Put the cursor back in the input field
	 	srcObject.focus();
 	}
}
</script>
</head>
<body>
<label for="inputfield">Enter A Number and Press 'Enter': </label>
<input type="text" name="inputField" id="inputField" onkeypress="runFunction(event);" />
</body>
</html>

Should get you started :)

Never be afraid to share your dreams with the world.
There's nothing the world loves more than the taste of really sweet dreams.

Enable Apps
 
Thanks for the reply dwarfthrower. I've done a bunch of fortran programming in the past, however, this stuff is not as intuitive to me. I will give it a go with the routine you have provided and see if I can make it work. Thanks again.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top