Changes

Building Forms with JavaScript

1,239 bytes added, 20:00, 15 May 2007
The JavaScript Text Object
* '''onSelect''' - triggered when the user highlights text in the text field.
 
In addition to events, the Text object also has a number of methods that can be accessed to perform such tasks as selecting text and changing focus:
 
* '''focus()''' - sets focus and sets the cursor on the text field
 
* '''blur()''' - Removes focus from the field (the opposite of focus())
 
* '''select()''' - Selects the text in the field so that when the user types all the existing text is replaced.
 
Now that we have covered some of the basics of building a FORM we can bring some of these concepts toegther in an example using the text object. The following example creates a Form containing a text field. When the user clicks in the text field the ''onFocus'' event is triggered which in turn calls the ''select()'' method to highlight the text in the field:
 
<pre>
 
<html>
<head>
<title>JavaScript Form Example</title>
<script language=javascript type="text/javascript">
function buttonPressed()
{
// document.write ( document.myForm.elements[0].value );
var phone = document.getElementById("myText");
 
document.write ( phone.value );
}
</script>
</head>
 
<body>
 
<form action="" name="myForm">
<input type="TEXT" name="myText" id="myText" value="Some text" onFocus="this.select()"/>
</form>
 
</body>
</html>
 
</pre>