CSS Chapter 3
CSS Chapter 3
HandlingUnit - III
•We can use this information within the web page, or it can be posted
to the web server where we can manipulate it and store it in a
database.
• The elements discussed in this chapter are the common elements made available
by HTML, and not ActiveX elements, Java Applets, or plug-ins.
• To create a form, use the <form> and </form> tags to declare where it starts and
where it ends. The
• the action attribute, which determines where the form is submitted to;
• the target attribute, which determines the frame to which the response to the
form is loaded.
•These tags create a Form object, which we can use to access the form.
This object can be accessed in two ways.
•One way to access these is through the elements property of the Form
object, another collection.
•The Form object also has the length property, which also gives you the
number of elements in the form.
•E.g.
document.myForm.length or
document.myForm.elements.length
•However, the Form object also has the submit() method, which does
nearly the same thing.
•The submit() method submits the form, but it does not fire the submit
event of the Form object;
•thus, the onsubmit event handler is not called when submitting the
form with submit().
Prepared By: Khan Mohammed Zaid, Lecturer, Comp. Engg.,
12
MHSSP
Properties & Methods of
•In addition toForms
there being a Reset button, the Form object has the
reset() method, which clears the form, or restores default values
if these exist.
• Standard Button
• Submit Button
• Reset Button
Prepared By: Khan Mohammed Zaid, Lecturer, Comp. Engg.,
14
MHSSP
HTML Elements in
Forms
•Common Properties:
• name: the value of this property can be used to refer that particular element
in your script. Also, if you are sending the information in the form to a
server, the element’s name property is sent along with any value of the
form element, so that the server knows what the value relates to.
• value: which returns the value of the element. Also, setting the value of the
value property enables you to put text inside the text box.
• type: Sometimes it’s useful to know what type of element you’re dealing with,
particularly where you’re looping through the elements in a form using
the elements collection property. This information can be retrieved by means
of
the type property, whichPrepared
each element’s object has.
By: Khan Mohammed Zaid, Lecturer, Comp. Engg.,
15
MHSSP
HTML Elements in
Forms
•Common Methods:
• focus(): If an element is the center of the focus, any key presses made by the
user will be passed directly to that element. For example, if a text box has
focus, pressing keys will enter values into the text box. Also, if a button
has the focus, pressing the Enter key will cause the button’s onclick event
handler code to fire, just as if a user had clicked the button with his
mouse.
• blur(): Blur, which perhaps could be better called “lost focus,” is the opposite of
focus. If you want to remove a form element from being the focus of the
user’s attention, you can use the blur() method. When used with a form
element, the blur() method usually results in the focus shifting to the page
containing the form.
Prepared By: Khan Mohammed Zaid, Lecturer, Comp. Engg.,
16
MHSSP
HTML Elements in
Forms
•Common Methods:
• In addition to the focus() and blur() methods, all the form element’s objects
have the onfocus and onblur event handlers. These are fired when an element
gets or loses the focus, respectively, due to user action or the focus() and
blur() methods.
•For example, to create a button called myButton, which has the words
“Click Me” on its face, the <input/> element would need to be as
follows:
•The type attribute is set to button, and the value attribute is set to the
text you want to appear on theface of the button.
•All you need to do is define a function that you want to have executed
when the button is clicked (say, button_onclick()) and then add the
onclick event handler as an attribute of the <input/> element as
follows:
<input type=”button” onclick=”button_onclick()” />
•mouseup and mousedown are triggered only when the mouse pointer
is actually over the element in question.
•They are defined just as you do a standard button, except that the type
attribute of the <input> tag is set to submit or reset rather than to
button.
•E.g.:
<input type=”submit” value=”Submit” name=”submit1” />
<input type=”reset” value=”Reset” name=”reset1” />
•When the Reset button is clicked, all the elements in a form are cleared
and returned to their default values (the values they had when the
page was first loaded).
•The size attribute determines how many characters wide the text box
is, and maxlength determines the maximum number of characters the
user can enter in the box.
•This may be used if the user has entered an invalid value, and you can
set the focus to the text box and select the text inside it. This then
puts the user’s cursor in the right place to correct the data and makes
it very clear to the user where the invalid data is.
•The onselect event fires when the user selects some text in the text
box.
•More useful is the onchange event, which fires when the element loses
focus if (and only if) the value inside the text box is different from the
value it had when it got the focus. This enables you to do things like
validity checks that occur only if something has changed.
•E.g.:
<input type=”text” name=”txtReadonly” value=”Look but don’t
change” onfocus=”document.form1.txtReadonly.blur()”
readonly=”readonly”>
•When sent to the server, the text in the password is sent as plain text
— there is no encryption or any attempt at hiding the text (unless the
page is served over a secure connection from the server).
•However, unlike the text box, the textarea element has its own tag,
the
<textarea> tag.
•It also has two additional attributes: cols and rows. The cols attribute
defines how many characters wide the text area will be, and the
rows attribute defines how many character rows there will be.
•You set the textinside the element by putting it between the start and
closing tags, rather than by using the value attribute.
Prepared By: Khan Mohammed Zaid, Lecturer, Comp. Engg.,
MHSSP
38
Text Area Box Element
•So if you want a <textarea/> element 40 characters wide by 20 rows
deep with initial text Hello World on the first line and Line 2 on
the second line, you define it as follows:
•Radio buttons are basically a group of check boxes where only one can
be checked at a time.
•Creating check boxes and radio buttons requires the <input/> element.
Its type attribute is set to “checkbox” or “radio” to determine which
box or button is created.
•To set a check box or a radio button to be checked when the page is
loaded, you simply insert the attribute checked into the <input> tag
and assign its value as checked.
Prepared By: Khan Mohammed Zaid, Lecturer, Comp. Engg.,
41
MHSSP
Check Box & Radio Button
•E.g. Element
<input type=“checkbox” name=“chkDVD” checked=“checked”
value=“DVD” />
•To create a group of radio buttons, you simply give each radio button
the same name. This creates an array of radio buttons going by that
name that you can access, as you would with any array, using its
index.
Prepared By: Khan Mohammed Zaid, Lecturer, Comp. Engg.,
42
MHSSP
Check Box & Radio Button
•Each checkElement
box has an associated Checkbox object, and each radio
button in a group has a separate Radio object.
•This property returns true if the check box is currently checked and
false if not.
} function checkblue()
{ document.getElementById("blue").checked = true;
} function uncheck()
{ document.getElementById("red").checked = false;
</html> document.getElementById("blue").checked = false; Zaid,
Prepared By: Khan Mohammed } Lecturer, Comp. Engg., 47
MHSSP
</script></body>
Selection
•Although they look quiteBoxes
different, the drop-down list and the list
boxes are actually both elements created with the <select> tag.
•The select element has one or more options in a list that you can select
from; each of these options is defined by means of one or more
<option/> elements inside the opening and closing <select> tags.
•The size attribute of the <select/> element is used to specify how many
of the options are visible to the user.
•The values of the options have been defined as numbers, but text
would be equally valid.
•To make it a drop-down list, we just need to change the size attribute in
the <select/> element to 1, and presto, it’s a drop-down list.
• string autocomplete: The string “on” or “off”. If “on”, the browser can prefill
form controls with saved values from a previous visit to the page.
• string method: The HTTP method used to submit the form to the action
URL.
Either “get” or “post”.
Prepared By: Khan Mohammed Zaid, Lecturer, Comp. Engg.,
58
MHSSP
Form Objects &
•Properties: elements
• string name: The name of the form, as specified by the HTML name attribute.
You can use the value of this property as a property name on the
document object. The value of that document property will be this Form
object.
• string target: The name of window or frame in which the document returned
by form submission is to be displayed.
• void Submit(): Submit the form manually, without triggering a Submit event.
• Sets the value of an attribute on the specified element. If the attribute already
exists, the value is updated; otherwise a new attribute is added with the
specified name and value.
where:
name: Required. Specifies name of the attribute whose value is to be set
value: Required. Specifies value to assign to the attribute.
• Syntax: element.getAttribute(name);
where:
name:Required. Specifies name of the attribute whose value is to be fetched
• Syntax: element.removeAttribute(name);
where:
name:Required. Specifies name of the attribute whose value is to be removed
• Syntax: element.hasAttribute(name);
where:
name:Required. Specifies name of the attribute you want to check if exists.
• Syntax: selectObject.remove(index)