JavaScript Scratches

Getting User Input

A common thing you will end up having to do is get some input from a user - a password, a filename, a search query, etc.

If you don't have or need access to CGI scripting, JavaScript may be sufficient for your needs.

Give me a number < 100 to play with:
 

That may look like a regular form but isn't. Typically, when you see something like that with a 'Submit' button, it triggers a script on a server, which then executes.

In the case here, the action when submitted is a JavaScript function run locally.

The text entry and button is some simple HTML with a JavaScript function tied to it when clicked. The response is displayed in an HTML div. View the source code to see that.

function DoMath() {
	const NumIn = document.getElementById('UserIn').value;
	if (NumIn >= 100) {
  		document.getElementById('NumOut').innerHTML = 'Nope. Too BIG';
  	}
  	else {
		if (NumIn < 100) {
		  let NumOut = Math.pow(NumIn,3) // cube the user entry
		  document.getElementById('NumOut').innerHTML = NumIn+'<sup>3</sup>: '+NumOut;

		}
	}
}