Title: JavaScript
Course: CPS 120: Introduction to Computer Science ~ Master (Aug/2015) OLD > Section: Learning Units

JavaScript

- Is a scripting language.
- Is used to make a web page independently interactive.
- Enables interactivity without the server reloading the web page.
- Is embedded within HTML using the script tag.
- May be used to:

✓Show pop-up messages.
✓Display and process menu selections.
✓Validate form input.
✓Perform calculations based on web page inputs.

- May be coded with a simple text editor.
- May be tested with a browser.

JavaScript call from button

- A JavaScript function, or block of code, may be connected to an HTML button.
- The onclick attribute of an HTML button connects the button to a function.

HTML text box value

- The value in an HTML text box may be read or set from a JavaScript function.
- The document.getElementById(‘id’).value command accesses the value of an HTML text box using its id attribute.

Example:

Arithmetic Operators

- Are used to perform arithmetic on values stored in HTML text boxes.
- Perform arithmetic on two numbers:

✓Two integers
✓Two real numbers
✓One integer, one real number

Operation | Operator
Addition* | +
Subtraction | -
Multiplication | *
Divide | /
Remainder after division | %

* Before addition is performed on the value in an HTML text box, it must be converted from a string to a number using either parseInt (string to integer) or parseFloat (string to real).