Basic DOM Tutorial: Use DOM to set the text box. Basic dom tutorial

Source: Internet
Author: User

Basic DOM Tutorial: Use DOM to set the text box. Basic dom tutorial

1. Control the number of characters entered by the user

For single-line text boxes and password input boxes, you can use the maxlength attribute to control the number of characters you enter.
For multi-line text, maxlength is a custom attribute, and the maximum number of characters can be entered. When an onkeypress event occurs, the LessThan () function returns the value. The function is as follows:

<Textarea name = "comments" id = "comments" cols = "40" rows = "4" maxlength = "50" onekeypress = "return LessThan (this ); "> </textarea>
Code details

Copy codeThe Code is as follows:
<Script language = "javascript">
Function LessThan (oTextArea ){
// Return the boolean value required for the number of characters in the text box
Return oTextArea. value. length <oTextArea. getAttribute ("maxlength ");
}
</Script>
<Form method = "post" name = "myForm1" action = "addInfo. aspx">
<P> <label for = "name"> enter your name: </label>
<Input type = "text" name = "name" id = "name" class = "txt" value = "name" maxlength = "10"> </p>
<P> <label for = "comments"> I want to leave a message: </label> <br>
<Textarea name = "comments" id = "comments" cols = "40" rows = "4" maxlength = "50" onkeypress = "return LessThan (this ); "> </textarea> </p>
<P> <input type = "submit" name = "btnSubmit" id = "btnSubmit" value = "Submit" class = "btn">
<Input type = "reset" name = "btnReset" id = "btnReset" value = "Reset" class = "btn"> </p>
</Form>

2. Set the mouse to automatically select text

First, automatically focus onmouseover = "this. focus" when the mouse passes"

Followed by onfocus = "this. select ()"
Code example:

Copy codeThe Code is as follows:
<Form method = "post" name = "form1" id = "form1" action = "addInfo. aspx">
<Input type = "text" name = "name" id = "name" class = "txt" value = "name" onmouseover = "this. focus () "onfocus =" this. select () ">
</Form>

You can use the following code to focus on multiple code instances:

Copy codeThe Code is as follows:
<Script type = "text/javascript">
Function myFocus (){
This. focus ();
}
Function mySelect (){
This. select ();
}
Window. onload = function (){
Var oForm = document. forms ["myForm1"];
OForm. name. onmouseover = myFocus;
OForm. name. onfocus = mySelect;
}
</Script>
<Form method = "post" name = "myForm1" action = "addInfo. aspx">
<P>
<Label for = "name"> enter your name: </label>
<Input type = "text" name = "name" id = "name" class = "txt" value = "name">
</P>
<P>
<Label for = "passwd"> enter your password: </label>
<Input type = "password" name = "passwd" id = "passwd" class = "txt">
</P>
<P>
<Input type = "submit" name = "btnSubmit" id = "btnSubmit" value = "Submit" class = "btn">
<Input type = "reset" name = "btnReset" id = "btnReset" value = "Reset" class = "btn">
</P>
</Form>

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.