Java_script Unit 3
Java_script Unit 3
Form Methods :
1] reset() – It is used to reset a form.
Syntax of Form:
Form Controls
</form>
It sends information via Form URL. It sends information via Form body.
1] Button Control –
It is a clickable controls.
It performs an action when it’s clicked.
Attributes :-
Disabled, onclick, autofocus.
Tag :-
<input type="button">
Program:
<!DOCTYPE html>
<html>
<head>
<title> Button Demo </title>
</head>
<body>
<form name="form1" id="fid">
<input type="button" value="submit" name="submit">
</form>
</body>
</html>
size,min,max,readonly,required,maxlength,placeholder,disabled.
Tag :-
<input type="text">
Program:
<!DOCTYPE html>
<html>
<head>
<title> Textbox Demo </title>
</head>
<body>
<form name="form1" id="fid">
Enter Your Name :
<input type="text" name="tf1" id="tid1">
<br><br>
<input type="button" value="submit" name="submit">
</form>
</body>
</html>
Program:
<!DOCTYPE html>
<html>
<head>
<title> TextArea Demo </title>
</head>
<body>
<form name="form1" id="fid">
Enter Your Feedback :
<br>
<textarea name="ta1" rows="5" cols="50"></textarea>
<br><br>
<input type="submit" value="Submit" name="submit">
</form>
</body>
</html>
<input type="password">
Program:
<!DOCTYPE html>
<html>
<head>
<title>Password Demo</title>
</head>
<body>
<form name="form1" id="fid">
Enter Password:
<input type="password" name="password" id="password" minlength="8"
maxlength="20" required>
<br><br>
<input type="submit" value="Submit" name="submit">
</form>
</body>
</html>
Program:
<!DOCTYPE html>
<html>
<head>
<title> Login Form Demo </title>
</head>
<body>
<form name="loginForm" id="loginForm">
<input type="checkbox">
<!DOCTYPE html>
<html>
<head>
<title> Checkbox Demo </title>
</head>
<body>
<form name="form1" id="fid">
<br><br>
<input type="submit" name="b1" value="Submit">
</form>
</body>
</html>
<input type="radio">
Program:
<!DOCTYPE html>
<html>
<head>
<title> RadioButton Demo </title>
</head>
<body>
<form name="form1" id="fid">
Select Your Gender :
<input type="radio" name="r1" value="Male"> Male
<input type="radio" name="r1" value="Female"> Female
<input type="radio" name="r1" value="Other"> Other
<br><br>
<input type="submit" name="b1" value="Submit">
</form>
</body>
</html>
<input type="date">
Program:
<!DOCTYPE html>
<html>
<head>
<title> Date Demo </title>
</head>
<body>
<form name="form1" id="fid">
Select Your Date of Birth :
<input type="date" name="date1" value="d1">
<br><Br>
<input type="submit" name="s1" value="Submit">
</form>
</body>
</html>
Program:
<!DOCTYPE html>
<html>
<head>
<title> Date Demo </title>
</head>
<body>
<form name="form1" id="fid">
Select Your Favorite Languages :
<br>
<select name="languages" multiple size="3">
<option value="C"> C </option>
<option value="C++"> C++ </option>
<option value="Java"> Java </option>
<option value="Python"> Python </option>
<option value="JavaScript"> JavaScript </option>
</select>
<br><br>
<input type="submit" name="s1" value="Submit">
</form>
</body>
</form>
</body>
</html>
function myFun() {
var name = document.getElementById('tid1').value;
alert("Your name is "+name);
}
</script>
</form>
</body>
</html>
function onFocus() {
alert("Form is in focus");
}
Program:
<html>
<form name="form1">
<input type="button" name="b1" value="Click Me" onmouseup="MouseUp()"
onmousedown="MouseDown()" onmouseover="MouseOver()" onmouseout="MouseOut()"
oncontextmenu="OnContextMenu()">
</form>
<script language="javascript" type="text/javascript">
function onPress()
{
document.forms.form1.tf1.value="Key is Pressed";
}
function onDown()
{
document.forms.form1.tf1.value-"Key is Down";
}
function onUp()
{
document.forms.form1.tf1.value="Key is Up";
}
</script>
</body>
</html>
Program:
<html>
<body>
<form name="form1" id="fid"> <center>
<h1> Login Form </h1>
<h3>Username :
<input type="text" name="tf1" id="t1">
<br> <br>
Password :
<input type="text" name="tf2" id="t2" >
<br> <br> <br>
<input type="submit" value="Submit" onclick="submitForm()">
<input type="reset" value="Reset" onclick="resetForm()">
</form>
<script language="javascript" type="text/javascript">
function submitForm() {
alert("Form data is submitted");
}
function resetForm() {
alert("Form data is Reset");
}
</script>
</body>
</html>
Program:
<html>
<body>
<form> <center>
<h1> <label> Login Form </label> </h1>
<h3><label> Username : </label>
<input type="text" name="tf1" placeholder="Enter Username">
<br> <br>
<label> Password : </label>
<input type="text" name="tf2" placeholder="Enter Password">
<br> <br><br>
<input type="button" name="b1" value="LOGIN" onclick="login()">
</form>
Program:
<html>
<body>
<form name="form1">
<center>
<h1> Calculator </h1>
Enter First Number :
<input type="text" name="tf1" id="t1">
<br><Br>
</script>
</body>
</html>
Program:
<html>
<body>
<script language="javascript" type="text/javascript">
function ChangeValue() {
document.forms.form1.tf1.style.backgroundColor="red";
document.forms.form1.tf1.style.color="blue";
</script>
<form name="form1" id="id1">
<input type="text" name="tf1" onkeypress="ChangeValue()">
</form>
</body>
</html>
Program:
<html>
<body>
<form name="form1" id="fid">
Enter Your Name : <input type="text" name="tf1" id="tid1" >
<br><br>
<input type="button" name="b1" value="Disable" onclick="ChangeState()">
</form>
</script>
</body>
</html>
After clicking on disable button, its label will get changed and textbox will be
disabled.
Program:
<html>
<body>
<form name="form1" id="fid">
</select>
</form>
function MySelection(val)
}
}
</script>
</body>
</html>
After clicking on other radio button, then OptionList will get changed.
Program:
<html>
<body>
<script language="javascript" type="text/javascript">
function Evaluate() {
with(document.forms.form1) {
var hobbies="Your Hobbies are : ";
if(c1.checked)
hobbies+=" Cricket";
if(c2.checked)
hobbies+=" Dancing";
if(c3.checked)
hobbies+=" Reading";
if(c4.checked)
hobbies+=" Walking";
</script>
</form>
</body>
</html>
Program:
<html>
<body>
<form name="form1" id="fid">
Enter Your Name : <input type="text" name="tf1" id="id1" >
<br>
Generated Password : <input type="text" name="tf2" id="id2" disabled = true>
<br>
<input type="button" value="Generate" name="b1" id="bid"
onclick="GeneratePassword()" >
</form>
Program:
<html>
<body>
<script language="javascript" type="text/javascript">
</script>
<form name="form1" id="id1">
Enter Username :
<input type="text" name="tf1">
<br>
Enter Password :
<input type="password" name="tf2";
<br><br>
<img src="login.jpg" onclick="javascript:document.forms.form1.submit(alert('form
Data Submitted'))" width="40" height="20">
<img src="reset.jpg" onclick="javascript:document.forms.form1.reset(alert('form
Data reseted'))" width="40" height="20">
</form>
</body>
</html>
Disabling Elements –
We can restrict some fields on the form by using disabled.
If disabled property of the form element is set to true, then the user
cannot edit that element.
Program:
<html>
<body>
<form name="form1" id="fid">
Enter Your Name : <input type="text" name="tf1" id="tid1" >
<br><br>
<input type="button" name="b1" value="Disable" onclick="ChangeState()">
</form>
</script>
</body>
</html>
After clicking on disable button, its label will get changed and textbox will be
disabled.
Program:
<html>
<head>
<script language="javascript" type="text/javascript">
function makeReadOnly() {
document.getElementById('t1').readOnly=true;
}
function removeReadOnly() {
document.getElementById('t1').readOnly=false;
}
</script>
</head>
<body>
<form name="form1" id="fid">
Enter Your Name : <input type="text" name="tf1" id="t1">
<br><br>
<input type="button" name="btn" value="Read Only" id="b1" onclick="makeReadOnly()">
<input type="button" name="btn1" value="Read & Write" id="b2" onclick="removeReadOnly()">
</form>
</body>
</html>