css notes 3rd
css notes 3rd
3.1 Building blocks of a Form, properties and methods of form, button, text, text area, checkbox, radio
button, select element.
1. Form is an html element which takes input from the user using various controls like
Textbox, Textarea ,Radio Buttons ,Checkboxes ,action list etc
2. The inputted data can be validated on client browser using javascript (e.g., required
fields are filled, email addresses are properly formatted, passwords are strong, etc.).
Performing validation on the client side provides immediate feedback to users, reducing
the chances of invalid data being sent to the server and enhancing user experience
3. When the user clicks the submit button, JavaScript will perform the validation checks. If
the data is valid, the form is then submitted to the server for further processing (e.g.,
saving the data to a database, processing payments).
4. HTML forms are mainly used for development of dynamic web pages
6. There may be other elements such as input, text area ,select and others inside the form
element
7. Syntax:
<form>
//other form Element
<form>
❖ Attributes of forms
2. Action : This specify the URL ( address ) where to submit the form For example:
action="submit_form.php"
3. Method : Determines (detect or explore )the HTTP method used to send form data when
submitting the form data to the server
i)GET : In GET method the form data is appended to the URL as a query string ,
this is default method. (suitable for non-sensitive data).
ii)Post : In post method the form data is not appended to the URL or Sends form
data in the body of the request (suitable for sensitive data).
Note : POST Request: When you fill out a form to buy a toy and click “Submit,”
the form data (like your name and toy request) is sent in the body of the request
to the server. The URL you see might look something like
www.example.com/buy—the details of what you’re buying are in the request
body, not the URL.
In the computer world, the request body is the part of an HTTP request that
contains the actual data being sent to the server.
1.
❖ Button Element using input Element
Attributes of <input>
1. Type
The type attribute of the <input> element determines (DETECTS) the type of control it
represents.
Eg.,
name: (Optional) Specifies a name for the button, which can be useful when handling form data.
Example: <input type="button" name="myButton" value="Click Me" />
1.Text Element :-
In HTML, the <input> element is used to create interactive controls for web-based forms to
accept user input. One common type of <input> element is the text input, which allows users
to enter a single line of text.
1. type
• Description: Specifies that the input field is a single-line text input.
• Value: "text"
• Example: <input type="text" />
2. name
• Description: Defines the name of the input element. This name is used when sending
form data and can be accessed via JavaScript.
• Example: <input type="text" name="username" />
3. value
• Description: Sets the initial value of the text input field. This value is displayed in the
input field when the page loads.
• Example: <input type="text" value="Default Text" />
4. Maxlength
• Description: Specifies the maximum number of characters that can be entered into the
input field.
• Example: <input type="text" maxlength="20" />
5. size
• Description: Defines the width of the input field in characters. This is a visual width, not a
limit on the number of characters that can be entered.
• Example: <input type="text" size="30" />
2.TextArea
The <textarea> element in HTML is used to create a multi-line text input field, allowing users to
enter larger amounts of text. It supports various attributes like rows, cols, placeholder,
and maxlength to customize its appearance and behavior. This element is commonly used for
comments, feedback, and other text input needs in forms
Key Attributes of <textarea>
1. cols:
2. rows:
3. name:
o Defines the name of the textarea, which is used to reference the form data
after submission.
4. placeholder:
o Provides a short hint that describes the expected value of the textarea.
o Example: <textarea placeholder="Enter your comments here..."></textarea>
5. maxlength:
o Specifies the maximum number of characters allowed in the textarea.
o Example: <textarea maxlength="200"></textarea>
6. wrap:
o Specifies how the text in the textarea is wrapped when submitted in a form.
o Values: soft (default, no wrapping), hard (wraps text),off:- input text is not
wrapped
o Example: <textarea wrap="hard"></textarea>
3. Checkbox Element (<input type="checkbox">):
A checkbox is an input element in HTML that allows the user to select one or more options from a set. It is
often used in forms when multiple choices are available.
2. name: Defines a name for the checkbox element, which is used when submitting form data.
3. value: Sets the value that will be sent to the server when the checkbox is checked/selected.
Each checkbox can have its own value.
4. checked: This attribute specifies that the checkbox should be checked by default when the
page loads.
5. id: Used to uniquely identify the checkbox. Often combined with the <label> element to
enhance usability by linking a label to the checkbox.
6. Eg :
<form>
<label for="vehicle1"> I have a bike</label>
<input type="checkbox" id="vehicle1" name="vehicle" value="Bike"><br>
Radio buttons are used when you want to allow a user to select one option from a group of choices,
1. type: Specifies the input type as radio, indicating that this is a radio button.
2. name: All radio buttons that belong to the same group must have the same name. This groups them
together, ensuring that only one button in that group can be selected at a time.
3. Value: Sets the value that will be sent to the server when the radio button is checked/selected.
4. checked: Specifies that a radio button should be selected by default when the page loads.
i. name Attribute:
• Purpose: The name attribute is used to identify the <select> element when the form is
submitted. This attribute ensures that the selected option(s) are sent to the server with a key-
value pair where the key is the value of the name attribute.
• Importance in Forms: Without the name attribute, the selected data from the dropdown will
not be submitted as part of the form data.
• EXAMPLE-
<select name="fruits">
<option value="apple">Apple</option>
<option value="banana">Banana</option>
<option value="grapes">Grapes</option>
</select>
• <option value="apple">Apple</option>:
o Key: "apple"
o Value: "Apple"
• Windows: The user can select multiple options by holding the Ctrl key.
• Mac: The user can select multiple options by holding the Cmd key.
• Example
<select name="languages" multiple>
<option value="python">Python</option>
<option value="javascript">JavaScript</option>
<option value="java">Java</option>
<option value="csharp">C#</option>
</select>
o Explanation: Here, the user can select multiple programming languages from the list by
holding Ctrl/Cmd or Shift.
• Purpose: Specifies the value that will be sent to the server when the form is submitted. If the
value attribute is not set, the text inside the <option> tag will be sent.
• Example:
<select name="fruits">
<option value="apple">Apple</option>
<option value="banana">Banana</option>
<option value="grapes">Grapes</option>
</select>
• Explanation: In this case, if the user selects "Apple," the form will send apple as the value to the
server, not the displayed text "Apple."
2. selected Attribute:
• Purpose: Pre-selects an option when the page loads. The option with the selected attribute will
be the default option when the form is displayed.
• Usage: Useful for indicating the default or most common choice.
• Example :
<select name="cars">
<option value="bmw">BMW</option>
<option value="audi" selected>Audi</option>
<option value="mercedes">Mercedes</option>
</select>
• Explanation: When the page loads, "Audi" will be the selected option by default.
Complete code
<option value="python">Python</option>
<option value="javascript">JavaScript</option>
<option value="java">Java</option>
<option value="csharp">C#</option>
</select>
</form>
• Methods of form
</head>
<body>
<form onsubmit="sub()">
Name : <input type="text" placeholder="type here" name="himanshu"
value="jadhav">
<br><br>
<input type="submit" name =" submit" value="submit">
</form>
</body>
</html>
• Onreset :
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>ONRESET</title>
<script>
function clean()
{
alert("data reset succesfully");
}
</script>
</head>
<body>
<form onreset="clean()">
Name :<input type="text" name="t1">
<input type="reset" name="himanshu" value="reset">
</form>
</body>
</html>
Events of form
i) Onchange
ii) Onselect
iii) Onblur
iv) Onfocous
1. onchange Event
Definition :
The onchange event occurs when the value of an input field changes and loses focus. It is commonly
used with <input>, <select>, and <textarea> elements. This event is triggered when the user makes a
change to the input and then either clicks away or presses the "Tab" key to leave the field.
Usage:
• The onchange event is used to perform an action or function when the user selects an option from
a dropdown or changes the value in a text input field.
• This event ensures that changes are detected and handled after the user has finished interacting
with the input element.
Example :
<DOCTYPE html>
<html>
<body>
<form>
</form>
</body>
</html>
Explanation:
• In this example, the alert message will appear only after the user changes the value in the text field
and moves the cursor away (on loss of focus).
• Real-World Use: Validating form fields like email or phone number after the user makes changes.
2. onselect Event
Definition:
The onselect event occurs when the user selects some text in an input field (either <input> or
<textarea>). This event can be used to detect when text within a text box is highlighted by the user.
Usage:
• This event is helpful when you need to provide some feedback or action when a user
selects text. For example, you might want to copy the selected text or change its style.
<!DOCTYPE html>
<html>
<body>
</body>
</html>
Explanation:
• When the user highlights or selects any part of the text inside the <textarea>, the alert box is
triggered.
• Real-World Use: Used in cases where selected text needs to be copied or highlighted for
further processing.
3. Onblur Event
Definition:
The onblur event occurs when an element loses focus. This is commonly used in form
validation to check the value of a field when the user moves away from it (for instance, tabbing
to the next field or clicking outside the element).
Usage:
• Useful in form validation, where you might want to check the correctness of an input after
the user finishes typing and moves away from the input field.
Example
<html>
<head>
<script>
function onblurex()
if(element.value=="")
</script>
</head>
<body>
<form>
</form>
</body>
</html>
Output :
Onfocous :
Defination :
“The onfocus event happens when you click on or tab into an input field. It’s used to highlight the field
or show extra information.”
Usage:
• Form fields: Often used with form inputs such as <input>, <textarea>, and <select> elements.
• User experience: Can be used to guide users by providing hints or altering the UI (like changing
the background color )
Example :
<html>
<head>
<title>foucout event</title>
<script>
function funex()
document.getElementById("ex").style.backgroundColor="cyan";
</script>
</head>
<body>
<form>
</form>
</body>
</html>
Ouput :
Mouse Event :
Mouse events are triggered when a user interacts with form elements using the mouse (clicking,
hovering, etc.).
1. Onclick
• When it happens : Fired when an elements is clicked .
• Common Use : used with checkbox , buttons , or any clickable elements.
• Example :
<form>
<label for="ex">enter the button</label>
<input type="button" name="himanshu" id="ex" onclick="alert('button click')" value="click me">
</form>
• Output :
• Explanation: When the user clicks the button, the alert box is shown.
2. Ondblclick:
•When It Happens: Fired when an element is double-clicked.
•Common Use: Often used for special actions, like opening an element in a new tab or editing
text.
• Example :
<label for="ex">enter the button</label>
<input type="button" name="himanshu" id="ex" ondblclick="alert('button
click')" value="click me">
• Output : same as above
• Explanation: When the user double-clicks the button, a message appears.
3. Onmouseup :
• When it happens : fired when the mouse button is released.
• Common use : used to detect when a click has been completed
• Examples : same as above just use this keyword
4. Onmousedown :
• When it happens : Fired when the mouse button is pressed down on an element.
• Common Use: Used to detect when a user starts clicking but hasn't released the click yet.
• Example : same example as above use the given keyword
• Explanation: When the user presses the mouse button down on the button, the event is triggered.
Common example :
<html>
<head>
<title>all mouse events</title>
</head>
<body>
<button onclick="alert('onclick')">onclick ahe me</button>
<br /><br />
<label for="sd"><b>note as same bki sobat pan possible ahe using input </b></label>
<input type="button" id="sd" value="me double click ahe" name="himanshu"
ondblclick="alert('double click')" />
</body>
</html>
output : bakinchyansathi pan asa op yeil code pramane
5. onmouseover Event
• When It Happens: The onmouseover event occurs when the user moves the mouse pointer onto
an element.
• Usage: This event is often used to change the appearance of an element (like a button) when the
user hovers over it.
6. onmouseout Event
When It Happens: The onmouseout event occurs when the mouse pointer moves away from an
element.
Usage: This event is used to:
• Undo changes made by the onmouseover event.
• Hide elements when the mouse moves away.
<head>
<script>
function over() {
document.frm1.b1.value = "mouseover";
function out() {
document.frm1.b1.value = "mouseout";
</script>
</head>
<body>
<form name="frm1">
</form>
</body>
</html>
Output:
7. Oncontextmenu :
• The oncontextmenu event occurs when the user right-clicks on an element.
• It is commonly used to disable the default right-click menu or to provide custom actions when
right-clicking on an element.
Example :
<html>
<head>
<title>Document</title>
<script>
function menu() {
alert("right click");
}
</script>
</head>
<body>
<form>
<input type="button" name="hima" value="click" oncontextmenu="menu()">
</form>
</body>
</html>
Output :
Key Events
Key events are triggered when the user interacts with a form using the keyboard (typing or pressing specific
keys).
1. Onkeydown :
• When It Happens: Fired when a key on the keyboard is pressed down.
• Common Use: Used for capturing keys when they are pressed, like shortcuts or special actions.
2. Onkeyup :
• When It Happens: Fired when a key is released after pressing it.
• Common Use: Used for actions that occur after the user has finished typing a key, like form
validation.
3. Onkeypress :
• When It Happens: Fired when a key is pressed down and is held (similar to onkeydown, but
more specific to character keys).
• Common Use: Used to detect when a character is entered into a field.
Common example:
<html>
<head>
<script>
function up() {
document.f1.updown.value = "keyup";
function down() {
document.f1.updown.value = 'keydown';
function press1() {
document.f1.press.value = "keypress";
</script>
</head>
<body>
<form name="f1">
</form>
</body>
</html>
Output :
ASSIGNMENT
8. What do you mean by evaluating program? checkbox selection with the help of a