Unit 4: Forms and JavaScript objects
Web Technologies notes · PTU syllabus (BSIT304)
On this page
Unit summary
Forms collect user data, and JavaScript objects process it. This unit covers the form object and its element methods — text, password, button, submit, reset, checkbox, radio, textarea, select and multi-choice lists — built-in objects String, Math and Date, and user-defined objects and instances.
After this unit you can
- Build forms with all element types
- Access form elements and methods in JavaScript
- Use String, Math and Date objects
- Create user-defined objects and instances
PTU syllabus topics
- The form object and its element methods (text, password, button, submit, reset, checkbox, radio, textarea, select and multi-choice lists)
- built-in objects (String, Math, Date)
- user-defined objects and instances
Text and password
<input type="text">
Choices
Checkbox, radio, select
Buttons
Submit, reset, button
Built-in objects
String, Math, Date
User-defined objects
Constructor functions and instances
Topic 1
The form object
html<form action="register.php" method="post">
...controls...
</form>- action: where the data is sent (a server script).
- method: GET sends data in the URL (visible, limited length, for searches); POST sends it in the request body (hidden, for passwords and large data).
Data location
In the URL
In the request body
Visibility
Visible
Not visible in the URL
Size
Limited
Large
Use
Search, filters
Login, registration, uploads
Topic 2
Text, password, button, submit and reset elements
| Control | Code | Purpose |
|---|---|---|
| Text box | <input type="text" name="uname"> | Single line of text |
| Password | <input type="password" name="pwd"> | Hides typed characters |
| Submit | <input type="submit" value="Register"> | Sends the form |
| Reset | <input type="reset"> | Clears all fields |
| Button | <input type="button" value="Check" onclick="check()"> | Runs a script |
Useful attributes: name (identifies the data), value, placeholder, maxlength, size and required.
Topic 3
Checkboxes and radio buttons
- Checkbox: choose any number of options.
<input type="checkbox" name="skills" value="C++"> C++ - Radio button: choose only one option from a group; all buttons in the group share the same name.
<input type="radio" name="gender" value="F"> Female checkedpre-selects an option.
Exam tip
Radio buttons in a group must have the same name — otherwise more than one can be selected. This is a common practical mistake.
Topic 4
Textarea, select and multi-choice lists
- Textarea: multi-line text such as an address:
<textarea name="address" rows="4" cols="40"></textarea> - Select (drop-down) list:
html<select name="course">
<option value="bca">BCA</option>
<option value="bba" selected>BBA</option>
</select>- Multi-choice select list: add
multiple(and oftensize) so the user can select several options with Ctrl.
Text input
Name, email
Password
Hidden typing
Radio
One of many
Checkbox
Many of many
Select
Drop-down choice
Textarea
Long text
Submit and reset
Send or clear
Topic 5
Form element methods and events
An event is something that happens on the page: click, change, submit, load, mouseover, keyup. JavaScript can respond to it.
html<form name="login" onsubmit="return validate()">
<input name="user"> <input type="password" name="pwd">
<input type="submit" value="Login">
</form>
<script>
function validate() {
const f = document.forms["login"];
if (f.user.value === "") { alert("Enter username"); return false; }
if (f.pwd.value.length < 6) { alert("Password too short"); return false; }
return true;
}
</script>Returning false from the submit handler stops the form from being sent.
Topic 6
Built-in objects: String, Math and Date
String methods
"SBS College".toUpperCase()
"SBS COLLEGE"
Array methods
[3,1,2].sort()
[1,2,3]
Date
new Date().getFullYear()
Current year
Math
Math.round(Math.random() × 10)
Random 0–10
JSON
JSON.stringify(obj) and JSON.parse(text)
Object ↔ text
- Objects:
let student = { name: "Ana", roll: 12 }; student.name; - Array methods: push, pop, shift, slice, splice, indexOf, map, filter, forEach, join.
- JSON (JavaScript Object Notation) is a text format for exchanging data with servers.
Topic 7
User-defined objects and instances
javascript// Object literal
const student = { name: "Aman", marks: 82, grade() { return this.marks >= 75 ? "A" : "B"; } };
// Constructor function and instances
function Book(title, price) {
this.title = title;
this.price = price;
this.discount = function (pct) { return this.price * (1 - pct / 100); };
}
const b1 = new Book("C Programming", 450);
const b2 = new Book("Web Design", 600);
console.log(b1.discount(10)); // 405
// ES6 class
class Account {
constructor(owner, balance) { this.owner = owner; this.balance = balance; }
deposit(amt) { this.balance += amt; return this.balance; }
}Key terms
- Form object
- JavaScript representation of an HTML form
- Radio button
- Option allowing one choice in a group
- Math object
- Built-in object with mathematical functions
- Constructor function
- Function creating objects with new
- Instance
- Individual object created from a constructor or class
Quick revision
- form attributes: action, method (GET, POST).
- text, password, button, submit, reset, checkbox, radio, textarea, select (multiple).
- Accessing values; focus, blur, submit, reset; validation.
- String (length, toUpperCase, indexOf, substring), Math (round, random, max), Date (getFullYear, getMonth).
- Object literals, constructor functions, classes, new.
Important exam questions
Practice questions written to the PTU exam pattern for this unit's syllabus: short answers (Section A style) and long answers (Sections B and C style).
Short-answer questions
- Q1.Distinguish GET and POST.
- Q2.Distinguish checkbox and radio button.
- Q3.How do you allow multiple selections in a select list?
- Q4.What does Math.random() return?
- Q5.How do you get the current year in JavaScript?
- Q6.What is the new keyword used for?
Long-answer questions
- Q1.Design an admission form using all form elements.
- Q2.Explain form element methods and validation with JavaScript.
- Q3.Explain String, Math and Date objects with examples.
- Q4.Explain user-defined objects and instances in JavaScript.
Stuck on this unit?
Message SBS on WhatsApp for help with Web Technologies, or to ask about studying B.Sc IT at Synetic.
