Chap 6 - JavaScript
Chap 6 - JavaScript
JavaScript
.
Introduction
• JavaScript
It is a programming language used for web
applications.
• Its code is written within an HTML program, hence
saved with extension .html
• <script> tag
is used for JavaScript programs.
• document.write
This is a function used to display results of JavaScript
in an HTML program.
E.g.
document.write(“This is my first JavaScript
program”);
Introduction cont.
• To display output (result) in text form
type = “text/javascript”
Program: firstJSprogram.html
<DOCTYPE html>
<html>
<body>
<script
type="text/javascript">
document.write(“This is my first JavaScript
program");
</script>
</body>
</html>
Output
Last name:
Submit
Components of the Button
type="button" id=“sbmtBtn" onclick=“sbmtFunction()">Submit
•“button” element is defined by type="button“, which
will create a button
•A variable sbmtBtn is used to create a button in
computer’s memory by id=“sbmtBtn“
•onclick=“sbmtFunction()>Submit: calls a function
sbmtFunction() to take an action and also labels the
button as Submit
Components of the Button cont…
function sbmtFunction()
•This is a JavaScript function which helps us to
create the button
•This function is called in the above HTML program
•In this function
• var x = document.getElementById(“sbmtBtn");
- reserves an area x in memory for the function
- sbmtBtn must be the same as given in the id of
HTML program
Combining Form with Button
<!DOCTYPE html>
<html>
<body>
<h2>Application Form for job of Web Engineer</h2>
<form>
<label for="fname">First name:</label><br>
<input type="text" id="fname" name="fname" value=""><br>
<label for="lname">Last name:</label><br>
<input type="text" id="lname" name="lname" value="">
// continued on next page
Combining Form with Button cont…
<button
type="button" id=“sbmtBtn" onclick=“sbmtFunction()">Submit
</button>
<script>
function sbmtFunction()
{
var x = document.getElementById(“sbmtBtn");
// Store data in a database on server – will be discussed later
}
</script>
</form>
</body>
</html>
Output
First name:
Last name:
Submit
.
End of Chap 6