JavaScript DHTML/Form Control/TextField
Содержание
- 1 Add textfield dynamically into HTML
- 2 Another TextField jump
- 3 Assign a default value to a Text object
- 4 Auto focus textfield
- 5 Block enter textfield
- 6 Call select method to highlight the text field
- 7 Change the text value in text box into upper case
- 8 Check if a text field is empty
- 9 Check if text field input is a number
- 10 Compound Interest Calculator
- 11 Data Validation via an onChange event Handler
- 12 Display textfield value in new page
- 13 Feed value from array to text box
- 14 Firing the onSame Event (FireFox)
- 15 Focus an input field
- 16 Get textfield value
- 17 Getting and Setting a Text Object"s Value
- 18 Getting and Setting a Text Object"s value Property
- 19 Get vcard name
- 20 If the textfield has been changed
- 21 Is Text Editable
- 22 JavaScript Loan Calculator
- 23 Jump to the next field
- 24 Letter only, yes no only textfield
- 25 Methods and Properties of the Text Object
- 26 Numerals Only
- 27 Passing a Text Object (as this) to the Function
- 28 Resetting a Text Object to Default Value
- 29 Selecting Text Upon Focus
- 30 Select the textfield and focus
- 31 Show TextField value in Dialog
- 32 Text Field autocomplete Example
- 33 TextField focus and select all
- 34 TextField focus, blur, and click action
- 35 TextField get Focus and clear content
- 36 Text field tab Index
- 37 Text Object Select and Focus
Add textfield dynamically into HTML
/*
JavaScript Bible, Fourth Edition
by Danny Goodman
John Wiley & Sons CopyRight 2001
*/
<HTML>
<HEAD>
<TITLE>mergeAttributes() Method</TITLE>
<SCRIPT LANGUAGE="JavaScript">
function doMerge(form) {
var newPElem = document.createElement("P")
var newInputElem = document.createElement("INPUT")
newInputElem.id = newInputElem.uniqueID
newInputElem.mergeAttributes(form.field1)
newPElem.appendChild(newInputElem)
form.appendChild(newPElem)
newInputElem.value = newInputElem.outerHTML
}
// called by onChange event handler of fields
function upperMe(field) {
field.value = field.value.toUpperCase()
}
</SCRIPT>
</HEAD>
<BODY onLoad="document.expandable.field1.value = document.expandable.field1.outerHTML">
<H1>mergeAttributes() Method</H1>
<HR>
<FORM NAME="expandable" onSubmit="return false">
<P><INPUT TYPE="button" VALUE="Append Field "Clone"" onClick="doMerge(this.form)"></P>
<P><INPUT TYPE="text" NAME="field1" ID="FIELD1" SIZE=120 VALUE="" STYLE="font-
size:9pt" onChange="upperMe(this)"></P>
</FORM>
</BODY>
</HTML>
Another TextField jump
<html><HEAD><TITLE>Jump Fields Demo</TITLE>
<script language=javascript>
<!--
function checklength(nextfield,chars,currfield) {
x= document.phone[currfield].value.length
if (x == chars) {
eval("document.phone." + nextfield + ".focus();");
}
}
//-->
</SCRIPT>
</HEAD>
<BODY>
<FORM name=phone>
( <INPUT value="" name=areacode maxlength="3" size="3" onkeyup="checklength("prefix",3,this.name)"> )
<INPUT value="" name=prefix maxlength="3" size="3" onkeyup="checklength("suffix",3,this.name)"> -
<INPUT value="" name=suffix maxlength="4" size="4">
</FORM></BODY></HTML>
Assign a default value to a Text object
<html>
<head>
<title>Assign a default value to a Text object</title>
<SCRIPT LANGUAGE="JavaScript">
<!--
function findBrowser() {
document.form1.Browser.value = navigator.appName
}
// -->
</SCRIPT>
</head>
<body onLoad="findBrowser()" >
<form name="form1" method="POST">
<h2>Assign a default value to a Text object</h2>
<p>Username:<br>
<input type=text size=25 maxlength=256 name="Username">
<br>Browser used:<br>
<input type=text size=25 maxlength=256 name="Browser">
<br>Email address:<strong> <br>
</strong>
<input type=text size=25 maxlength=256 name="EmailAddress"></p>
<h2>
<input type=submit value="Register">
<input type=reset value="Clear">
</h2>
</form>
</body>
</html>
Auto focus textfield
<html>
<head>
<SCRIPT LANGUAGE="JavaScript">
function autofocus(field, limit, next, evt) {
evt = (evt) ? evt : event;
var charCode = (evt.charCode) ? evt.charCode : ((evt.keyCode) ? evt.keyCode :
((evt.which) ? evt.which : 0));
if (charCode > 31 && field.value.length == limit) {
field.form.elements[next].focus();
}
}
</SCRIPT>
</head>
<body>
<form>
Credit Card Number:
<input type="text" name="cc1" size="5" maxlength="4"
onkeypress="return numeralsOnly(event)"
onkeyup="autofocus(this, 4, "cc2", event)">
<input type="text" name="cc2" size="5" maxlength="4"
onkeypress="return numeralsOnly(event)"
onkeyup="autofocus(this, 4, "cc3", event)">
<input type="text" name="cc3" size="5" maxlength="4"
onkeypress="return numeralsOnly(event)"
onkeyup="autofocus(this, 4, "cc4", event)">
<input type="text" name="cc4" size="5" maxlength="4"
onkeypress="return numeralsOnly(event)">
</form>
</body>
</html>
Block enter textfield
<html>
<head>
<SCRIPT LANGUAGE="JavaScript">
function blockEnter(evt) {
evt = (evt) ? evt : event;
var charCode = (evt.charCode) ? evt.charCode :((evt.which) ? evt.which : evt.keyCode);
if (charCode == 13) {
return false;
} else {
return true;
}
}
</SCRIPT>
</head>
<body>
<form>
<input type="text" name="search" size="40" onkeydown="return blockEnter(event)" />
</form>
</body>
</html>
Call select method to highlight the text field
<HTML>
<HEAD>
<SCRIPT LANGUAGE=JavaScript>
function butCheckForm_onclick()
{
var myForm = document.form1;
myForm.txtAge.blur();
if (myForm.txtAge.value == "" || myForm.txtName.value == "")
{
document.write("missing value!");
if (myForm.txtName.value == "")
{
myForm.txtName.focus();
}
else
{
myForm.txtAge.focus();
}
}
else
{
document.write(myForm.txtName.value);
}
}
function txtAge_onblur()
{
var txtAge = document.form1.txtAge;
if (isNaN(txtAge.value) == true)
{
alert("Age must be a number.");
txtAge.focus();
txtAge.select();
}
}
function txtName_onchange()
{
window.status = "Hi " + document.form1.txtName.value;
document.write(document.form1.txtName.value);
}
</SCRIPT>
</HEAD>
<BODY>
<FORM NAME=form1> Please enter the following details:
Name:<INPUT TYPE="text" NAME=txtName onchange="txtName_onchange()">
Age: <INPUT TYPE="text" NAME=txtAge onblur="txtAge_onblur()">
<INPUT TYPE="button" VALUE="Check Details" NAME=butCheckForm onclick="butCheckForm_onclick()">
</FORM>
</BODY>
</HTML>
Change the text value in text box into upper case
<html>
<head>
<title>Text Object Value</title>
<script type="text/javascript">
function upperMe() {
document.getElementById("output").value = document.getElementById("input").value.toUpperCase();
}
</script>
</head>
<body>
Enter lowercase letters for conversion to uppercase:<br>
<form name="converter">
<input type="text" name="input" id="input"
value="sample" onchange="upperMe()" /><br />
<input type="text" name="output" id="output" value="" />
</form>
</body>
</html>
Check if a text field is empty
<HTML>
<HEAD>
<SCRIPT LANGUAGE=JavaScript>
function butCheckForm_onclick()
{
var myForm = document.form1;
myForm.txtAge.blur();
if (myForm.txtAge.value == "" || myForm.txtName.value == "")
{
document.write("missing value!");
if (myForm.txtName.value == "")
{
myForm.txtName.focus();
}
else
{
myForm.txtAge.focus();
}
}
else
{
document.write(myForm.txtName.value);
}
}
function txtAge_onblur()
{
var txtAge = document.form1.txtAge;
if (isNaN(txtAge.value) == true)
{
alert("Age must be a number.");
txtAge.focus();
txtAge.select();
}
}
function txtName_onchange()
{
window.status = "Hi " + document.form1.txtName.value;
document.write(document.form1.txtName.value);
}
</SCRIPT>
</HEAD>
<BODY>
<FORM NAME=form1> Please enter the following details:
Name:<INPUT TYPE="text" NAME=txtName onchange="txtName_onchange()">
Age: <INPUT TYPE="text" NAME=txtAge onblur="txtAge_onblur()">
<INPUT TYPE="button" VALUE="Check Details" NAME=butCheckForm onclick="butCheckForm_onclick()">
</FORM>
</BODY>
</HTML>
Check if text field input is a number
<HTML>
<HEAD>
<SCRIPT LANGUAGE=JavaScript>
function butCheckForm_onclick()
{
var myForm = document.form1;
myForm.txtAge.blur();
if (myForm.txtAge.value == "" || myForm.txtName.value == "")
{
document.write("missing value!");
if (myForm.txtName.value == "")
{
myForm.txtName.focus();
}
else
{
myForm.txtAge.focus();
}
}
else
{
document.write(myForm.txtName.value);
}
}
function txtAge_onblur()
{
var txtAge = document.form1.txtAge;
if (isNaN(txtAge.value) == true)
{
alert("Age must be a number.");
txtAge.focus();
txtAge.select();
}
}
function txtName_onchange()
{
window.status = "Hi " + document.form1.txtName.value;
document.write(document.form1.txtName.value);
}
</SCRIPT>
</HEAD>
<BODY>
<FORM NAME=form1> Please enter the following details:
Name:<INPUT TYPE="text" NAME=txtName onchange="txtName_onchange()">
Age: <INPUT TYPE="text" NAME=txtAge onblur="txtAge_onblur()">
<INPUT TYPE="button" VALUE="Check Details" NAME=butCheckForm onclick="butCheckForm_onclick()">
</FORM>
</BODY>
</HTML>
Compound Interest Calculator
<html>
<head>
<title>Compound Interest Calculator</title>
<script language="JavaScript">
function calculate(formObj)
{
var presentVal = parseFloat(formObj.presentVal.value);
var intRate = parseFloat(formObj.intRate.value)/100.;
var years = parseFloat(formObj.years.value);
var futureVal = presentVal * Math.pow((1.0+intRate),years);
var totalInt = futureVal - presentVal;
futureVal = Math.round(futureVal*100.0)/100.0;
totalInt = Math.round(totalInt*100.0)/100.0;
formObj.futureVal.value = futureVal;
formObj.totalInt.value = totalInt;
return;
}
</script>
</head>
<body>
<h1>Compound Interest Calculator</h1>
<form name="myform">
<pre>
Present Value: <input type="text" name="presentVal">
Interest Rate(%): <input type="text" name="intRate">
Years of Compounding: <input type="text" name="years">
<input type="button" name="calc" value="Calculate" onclick="calculate(myform)">
</pre>
<hr>
<pre>
Total Interest: <input type="text" name="totalInt">
Future Value: <input type="text" name="futureVal">
<pre>
</form>
</body>
Data Validation via an onChange event Handler
<HTML>
<HEAD>
<TITLE>Text Object Select/Focus</TITLE>
<SCRIPT LANGUAGE="JavaScript">
function isNumber(inputStr) {
for (var i = 0; i < inputStr.length; i++) {
var oneChar = inputStr.substring(i, i + 1)
if (oneChar < "0" || oneChar > "9") {
alert("Please make sure entries are numbers only.")
return false
}
}
return true
}
function checkIt(form) {
inputStr = form.numeric.value
if (isNumber(inputStr)) {
// statements if true
} else {
form.numeric.focus()
form.numeric.select()
}
}
</SCRIPT>
</HEAD>
<BODY onSubmit="checkIt(this); return false">
<FORM>
Enter any positive integer: <INPUT TYPE="text" NAME="numeric"
onChange="checkIt(this.form)"><P>
</FORM>
</BODY>
</HTML>
Display textfield value in new page
<html>
<head>
<title>Welcome Page</title>
<script type="text/javascript">
function rewritePage(form) {
var newPage = "<html><head><title>Page for ";
newPage += form.entry.value;
newPage += "</title></head><body>";
newPage += "<h1>Hello, " + form.entry.value + "!</h1>";
newPage += "</body></html>";
document.write(newPage);
document.close();
}
</script>
<body>
<h1>Welcome!</h1>
<hr>
<form onsubmit="return false;">
<p>Enter your name here: <input type="text" name="entry" id="entry"></p>
<input type="button" value="New Custom Page" onclick="rewritePage(this.form);">
</form>
</body>
</html>
Feed value from array to text box
<html>
<head>
<title>Simple Array Demo</title>
<script>
var counter = 0;
function upDate(){
counter++;
if (counter > 3){
counter = 0;
}
var description = new Array(3)
description[0] = "A";
description[1] = "B";
description[2] = "C";
description[3] = "D";
document.myForm.txtDescription.value = description[counter];
}
</script>
</head>
<body onLoad = "initialize()">
<center>
<form name = "myForm">
<input type = "text" value = "A" name = "txtDescription">
<input type = "button" value = "next" onClick = "upDate()">
</form>
</center>
</body>
</html>
Firing the onSame Event (FireFox)
<HTML>
<HEAD>
<TITLE>An event of my own!</TITLE>
<SCRIPT>
function MyClass (name, text1, text2) {
this.name = name;
this.text1 = text1;
this.text2 = text2;
}
MyClass.prototype.toString = function () {
return this.name;
}
function on_Same () {
alert("The two values entered in " + this.toString() + " are the same!");
}
function check_Same() {
if (this.text1 == this.text2) {
this.onSame();
}
}
MyClass.prototype.checkSame = check_Same;
MyClass.prototype.onSame = on_Same;
function createMyClass (name, text1, text2) {
var x = new MyClass (name, text1, text2);
x.checkSame();
}
</SCRIPT>
</HEAD>
<BODY>
<TABLE>
<FORM>
<TR>
<TD>Name your object:</TD>
<TD>
<input type=text
name="txtName">
</TD>
</TR>
<TR>
<TD>Enter first text:</TD>
<TD>
<input type=text name="txtFirst">
</TD>
</TR>
<TR>
<TD>Enter second text:</TD>
<TD>
<input type=text
name="txtSecond">
</TD>
</TR>
<TR>
<TD>
</TD>
<TD>
<input type=button value="Do It!" onClick="createMyClass (txtName.value,txtFirst.value, txtSecond.value);">
</TD>
</TR>
</FORM>
</TABLE>
</BODY>
</HTML>
Focus an input field
<html>
<head>
<script type="text/javascript">
function setfocus(){
document.forms[0].field.focus()
}
</script>
</head>
<body>
<form>
<input type="text" name="field" size="30">
<input type="button" value="Set focus" onclick="setfocus()">
</form>
</body>
</html>
Get textfield value
<html>
<head>
<script type="text/javascript">
<!--
function convertText() {
document.SampleForm.myText.value = document.SampleForm.myText.value.toUpperCase()
}
-->
</script>
</head>
<body>
<form name="SampleForm">
<input type="text" name="myText">
<input type="button" value="Convert" onclick="convertText()">
</form>
</body>
</html>
Getting and Setting a Text Object"s Value
<HTML>
<HEAD>
<TITLE>Text Object Value</TITLE>
<SCRIPT LANGUAGE="JavaScript">
function upperMe(form) {
inputStr = form.converter.value
form.converter.value = inputStr.toUpperCase()
}
</SCRIPT>
</HEAD>
<BODY>
<FORM onSubmit="window.focus(); return false">
Enter lowercase letters for conversion to uppercase:
<INPUT TYPE="text" NAME="converter" VALUE="sample" onChange="upperMe(this.form)">
</FORM>
</BODY>
</HTML>
Getting and Setting a Text Object"s value Property
<html>
<head>
<title>Text Object value Property</title>
<script type="text/javascript">
function upperMe() {
var field = document.forms[0].converter;
var upperCaseVersion = field.value.toUpperCase();
field.value = upperCaseVersion;
}
</script>
</head>
<body>
<form onsubmit="return false">
<input type="text" name="converter" value="sample" onchange="upperMe()">
</form>
</body>
</html>
Get vcard name
<html>
<body>
<input id="myText" type="text" name="firstName">
<script language="JavaScript">
document.all.myText.vcard_name = "vCard.FirstName";
</script>
</body>
</html>
If the textfield has been changed
<html>
<head>
<script language="JavaScript">
<!--
function taChange() {
alert("The text-area field has changed.");
}
//-->
</script>
</head>
<body>
<form>
Enter some text:
<input type="text" size=35 name="sometext"><p>Enter some more text:<br>
<textarea name="tarea" rows=5 cols=40 onChange="taChange()">
</textarea>
</form>
</body>
</html>
Is Text Editable
<html>
<body>
<script language="JavaScript">
function function1(elem) {
elem ? alert("Yes") : alert("No");
}
</script>
<input type="text" id="myInput" value="Input element">
<textarea id="myTxtArea">Textarea element</textarea>
<p id="myP">P element</p>
<br>
<button onclick="function1(myInput.isTextEdit);">Input</button>
<button onclick="function1(myTxtArea.isTextEdit);">TextArea</button>
<button onclick="function1(myP.isTextEdit);">Paragraph</button>
</body>
</html>
JavaScript Loan Calculator
/*
Examples From
JavaScript: The Definitive Guide, Fourth Edition
Legal matters: these files were created by David Flanagan, and are
Copyright (c) 2001 by David Flanagan. You may use, study, modify, and
distribute them for any purpose. Please note that these examples are
provided "as-is" and come with no warranty of any kind.
David Flanagan
*/
<head><title>JavaScript Loan Calculator</title></head>
<body bgcolor="white">
<!--
This is an HTML form that allows the user to enter data, and allows
JavaScript to display the results it computes back to the user. The
form elements are embedded in a table to improve their appearance.
The form itself is given the name "loandata", and the fields within
the form are given names like "interest" and "years". These
fieldnames are used in the JavaScript code that follows the form.
Note that some of the form elements define "onchange" or "onclick"
event handlers. These specify strings of JavaScript code to be
executed when the user enters data or clicks on a button.
-->
<form name="loandata">
<table>
<tr><td colspan="3"><b>Enter Loan Information:</b></td></tr>
<tr>
<td>1)</td>
<td>Amount of the loan (any currency):</td>
<td><input type="text" name="principal" size="12"
onchange="calculate();"></td>
</tr>
<tr>
<td>2)</td>
<td>Annual percentage rate of interest:</td>
<td><input type="text" name="interest" size="12"
onchange="calculate();"></td>
</tr>
<tr>
<td>3)</td>
<td>Repayment period in years:</td>
<td><input type="text" name="years" size="12"
onchange="calculate();"></td>
</tr>
<tr><td colspan="3">
<input type="button" value="Compute" onclick="calculate();">
</td></tr>
<tr><td colspan="3">
<b>Payment Information:</b>
</td></tr>
<tr>
<td>4)</td>
<td>Your monthly payment will be:</td>
<td><input type="text" name="payment" size="12"></td>
</tr>
<tr>
<td>5)</td>
<td>Your total payment will be:</td>
<td><input type="text" name="total" size="12"></td>
</tr>
<tr>
<td>6)</td>
<td>Your total interest payments will be:</td>
<td><input type="text" name="totalinterest" size="12"></td>
</tr>
</table>
</form>
<!--
This is the JavaScript program that makes the example work. Note that
this script defines the calculate() function called by the event
handlers in the form. The function refers to values in the form
fields using the names defined in the HTML code above.
-->
<script language="JavaScript">
function calculate() {
// Get the user"s input from the form. Assume it is all valid.
// Convert interest from a percentage to a decimal, and convert from
// an annual rate to a monthly rate. Convert payment period in years
// to the number of monthly payments.
var principal = document.loandata.principal.value;
var interest = document.loandata.interest.value / 100 / 12;
var payments = document.loandata.years.value * 12;
// Now compute the monthly payment figure, using esoteric math.
var x = Math.pow(1 + interest, payments);
var monthly = (principal*x*interest)/(x-1);
// Check that the result is a finite number. If so, display the results
if (!isNaN(monthly) &&
(monthly != Number.POSITIVE_INFINITY) &&
(monthly != Number.NEGATIVE_INFINITY)) {
document.loandata.payment.value = round(monthly);
document.loandata.total.value = round(monthly * payments);
document.loandata.totalinterest.value =
round((monthly * payments) - principal);
}
// Otherwise, the user"s input was probably invalid, so don"t
// display anything.
else {
document.loandata.payment.value = "";
document.loandata.total.value = "";
document.loandata.totalinterest.value = "";
}
}
// This simple method rounds a number to two decimal places.
function round(x) {
return Math.round(x*100)/100;
}
</script>
</body>
</html>
Jump to the next field
<html>
<head>
<script type="text/javascript">
function toUnicode(elmnt,content){
if (content.length==elmnt.maxLength){
next=elmnt.tabIndex
if (next<document.forms[0].elements.length){
document.forms[0].elements[next].focus()
}
}
}
</script>
</head>
<body>
<form>
<input size="3" tabindex="1" maxlength="3" onkeyup="toUnicode(this,this.value)">
<input size="3" tabindex="2" maxlength="3" onkeyup="toUnicode(this,this.value)">
<input size="3" tabindex="3" maxlength="3" onkeyup="toUnicode(this,this.value)">
</form>
</body>
</html>
Letter only, yes no only textfield
<HTML>
<HEAD>
<TITLE>Letter only, yes no only textfield </TITLE>
<SCRIPT>
function lettersOnly(evt) {
evt = (evt) ? evt : event;
var charCode = (evt.charCode) ? evt.charCode : ((evt.keyCode) ? evt.keyCode :
((evt.which) ? evt.which : 0));
if (charCode > 31 && (charCode < 65 || charCode > 90) &&
(charCode < 97 || charCode > 122)) {
alert("Enter letters only.");
return false;
}
return true;
}
function ynOnly(evt) {
evt = (evt) ? evt : event;
var charCode = (evt.charCode) ? evt.charCode : ((evt.keyCode) ? evt.keyCode :
((evt.which) ? evt.which : 0));
if (charCode > 31 && charCode != 78 && charCode != 89 &&
charCode != 110 && charCode != 121) {
alert("Enter \"Y\" or \"N\" only.");
return false;
}
return true;
}
</script>
</head>
<body>
<form>
Signature Present: <input type="text" name="signature" size="2" maxlength="1"
onkeypress="return ynOnly(event)" /> (Y/N)
</form>
</body>
</html>
Methods and Properties of the Text Object
Method
blur() Removes the focus from the text box.
focus() Gives the focus to the text box.
handleEvent() Invokes the default handler for the specified event.
select() Selects the text in the text box.
Property
defaultValue Returns the value of this text box, specified by the value attribute.
form Returns the entire form the text box is in.
name Returns the name, specified by the name attribute.
type Returns the type specified by the type attribute.
value Returns the value that is actually displayed in the text box.
Numerals Only
<HTML>
<HEAD>
<TITLE>Numerals Only</TITLE>
<SCRIPT>
function numeralsOnly(evt) {
evt = (evt) ? evt : event;
var charCode = (evt.charCode) ? evt.charCode : ((evt.keyCode) ? evt.keyCode :
((evt.which) ? evt.which : 0));
if (charCode > 31 && (charCode < 48 || charCode > 57)) {
alert("Enter numerals only in this field.");
return false;
}
return true;
}
</script>
</head>
<body>
<form>
<input type="text" ... onkeypress="return numeralsOnly(event)">
</form>
</body>
</html>
Passing a Text Object (as this) to the Function
<HTML>
<HEAD>
<TITLE>Text Object Value</TITLE>
<SCRIPT LANGUAGE="JavaScript">
function upperMe(field) {
field.value = field.value.toUpperCase()
}
</SCRIPT>
</HEAD>
<BODY>
<FORM onSubmit="window.focus(); return false">
Enter lowercase letters for conversion to uppercase:
<INPUT TYPE="text" NAME="converter" VALUE="sample" onChange="upperMe(this)">
</FORM>
</BODY>
</HTML>
Resetting a Text Object to Default Value
<HTML>
<HEAD>
<TITLE>Text Object DefaultValue</TITLE>
<SCRIPT LANGUAGE="JavaScript">
function upperMe(field) {
field.value = field.value.toUpperCase()
}
function resetField(form) {
form.converter.value = form.converter.defaultValue
}
</SCRIPT>
</HEAD>
<BODY>
<FORM onSubmit="window.focus(); return false">
Enter lowercase letters for conversion to uppercase:
<INPUT TYPE="text" NAME="converter" VALUE="sample" onChange="upperMe(this)">
<INPUT TYPE="button" VALUE="Reset Field"
onClick="resetField(this.form)">
</FORM>
</BODY>
</HTML>
Selecting Text Upon Focus
<html>
<head>
<title>Selecting Text Upon Focus</title>
</head>
<body>
<form name="form1" method="POST">
<h2>Selecting Text Upon Focus</h2>
<p>Username:<br>
<input type=text size=25 maxlength=256 name="Username" onFocus="this.select()"><br>
Browser used:<br>
<input type=text size=25 maxlength=256 name="Browser" onFocus="this.select()"><br>
Email address:
<strong> <br>
</strong>
<input type=text size=25 maxlength=256 name="EmailAddress"onFocus="this.select()">
</p>
<h2>
<input type=submit value="Register">
<input type=reset value="Clear">
</h2>
</form>
</body>
</html>
Select the textfield and focus
<html>
<head>
<script type="text/javascript">
function setfocus(){
document.forms[0].txt.select()
document.forms[0].txt.focus()
}
</script>
</head>
<body>
<form>
<input type="text" name="txt" size="30" value="Hello World!">
<input type="button" value="Select text" onclick="setfocus()">
</form>
</body>
</html>
Show TextField value in Dialog
<html>
<head>
<script language="JavaScript">
<!--
function sendData(arg) {
alert("The " + arg.name + " field has changed.");
}
//-->
</script>
</head>
<body>
<form>
<table>
<tr>
<td>Name:</td>
<td><input name="persname" type="text" onChange="sendData(this)"></td>
</tr>
<tr>
<td>E-mail:</td>
<td><input name="email" type="text"></td>
</tr>
</table>
</form>
</body>
</html>
Text Field autocomplete Example
<html>
<head>
<script>
function function1() {
document.all.myName.autocomplete = "on";
}
</script>
</head>
<body onLoad="function1();">
<form name="form1" method="post" action="">
<pre>
1. Type your name in the text field.
2. Press "Submit" button.
3. Type the first letter of your name again.
4. Check the autofill feature.
</pre>
<input type="text" size=30 name="text2" id="myName">
<input type="submit" value="Submit">
</form>
</body>
</html>
TextField focus and select all
<html>
<head>
<script language="JavaScript">
<!--
function checkField(formName) {
if (formName.f1.value != "Joe") {
document.forms[0].f1.focus();
document.forms[0].f1.select();
}
}
//-->
</script>
</head>
<body>
<form>
<pre>
Field1 <input name="f1" onBlur="checkField(this.form)"><br>
Field2 <input name="f2">
</pre>
</form>
</body>
</html>
TextField focus, blur, and click action
<HTML>
<HEAD>
<BODY>
<A HREF="http://www.wbex.ru" onMouseOver="window.alert("Sorry!");">
wbex.ru!</A>
<P>
<FORM>
Phone Number:
<INPUT type="text" size="25" onFocus="window.alert("Format is xxx-xxxx.");">
<BR>
Name:
<INPUT type="text" size="25" onBlur="window.alert("onBlur!");">
<BR>
E-mail Address:
<INPUT type="text" size="25">
<P>
<INPUT type="button" value="Click Here." onClick="window.alert("onClick")";>
</FORM>
</BODY>
</HTML>
TextField get Focus and clear content
<html>
<head>
<title>Clear Form Demo</title>
<script language="javascript">
<!--
function ClearForm() {
document.testform.email.value= "";
}
//-->
</script>
</head>
<body>
<form name="testform">
<input type="text" name="email" value="Something here." onfocus="ClearForm();">
</form>
</body>
</html>
Text field tab Index
<html>
<body>
<input type="text" value="Tab 1" tabindex="0"</input>
<input id="myText" type="text" value="Tab 2" tabindex="1"</input>
<button onclick="alert(myText.tabIndex);">Tab Index of second text box</button>
</body>
</html>
Text Object Select and Focus
<HTML>
<HEAD>
<TITLE>Text Object Select/Focus</TITLE>
<SCRIPT LANGUAGE="JavaScript">
function isNumber(inputStr) {
for (var i = 0; i < inputStr.length; i++) {
var oneChar = inputStr.charAt(i)
if (oneChar < "0" || oneChar > "9") {
alert("Please make sure entries are integers only.")
return false
}
}
return true
}
function checkNumeric(fld) {
var inputStr = fld.value
if (isNumber(inputStr)) {
// statements if true
} else {
setTimeout("doSelection(document." + fld.form.name + ". " + fld.name + ")", 0)
}
}
function doSelection(fld) {
fld.focus()
fld.select()
}
</SCRIPT>
</HEAD>
<BODY>
<FORM NAME="entryForm" onSubmit="return false">
Enter any positive integer: <INPUT TYPE="text" NAME="numeric"><P>
<INPUT TYPE="button" VALUE="Verify" onClick="checkNumeric(this.form.numeric)">
</FORM>
</BODY>
</HTML>