Dom (ix) Setting a text box with the DOM

Source: Internet
Author: User

1. Control the number of characters entered by the user
For single-line text boxes and password entry boxes, you can use the MaxLength property to control the number of characters entered by the user.
For multiline text, MaxLength is a custom attribute with a value of up to the number of characters entered, and the onkeypress event occurs when the return value of the LessThan () function is returned, the function is as follows

<name= "comments"  ID= "comments"  cols= "Max" rows = "4" maxlength = " the"  = "return LessThan (this);" ></ textarea >

Detailed code

<script language= "JavaScript" >function LessThan (Otextarea) {//returns whether the number of characters in the text box is the Boolean value required for the symbol return OTextArea.value.length < Otextarea.getattribute ("MaxLength");} </script><form method= "POST" name= "MyForm1" action= "addinfo.aspx" ><p><label for= "name" > Please enter your name: </label><input type= "text" name= "name" id= "name" class= "TXT" value= "name" Maxlength= "Ten" ></p ><p><label for= "Comments" > I want to leave a message: </label><br><textarea name= "Comments" id= "comments" Cols= "rows=" 4 "maxlength=" 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

The first is the auto focus when the mouse is over onmouseover = "This.focus"

Next is onfocus = "this.select ()"
code example:

<formMethod= "POST"name= "Form1"ID= "Form1"Action= "Addinfo.aspx"><inputtype= "text"name= "Name"ID= "Name"class= "txt"value= "Name"onmouseover= "This.focus ()"onfocus= "This.select ()"></form>

For multiple code instances, you can use the following code to focus

<Scripttype= "Text/javascript">            functionMyfocus () { This. focus (); }            functionMyselect () { This. Select (); } window.onload= function() {                varOform=document.forms["MyForm1"]; OForm.name.onmouseover=Myfocus; OForm.name.onfocus=Myselect; }        </Script>            <formMethod= "POST"name= "MyForm1"Action= "Addinfo.aspx">                <P>                    <label for= "Name">Please enter your name:</label>                    <inputtype= "text"name= "Name"ID= "Name"class= "txt"value= "Name">                </P>                <P>                    <label for= "passwd">Please enter your password:</label>                    <inputtype= "Password"name= "passwd"ID= "passwd"class= "txt">                </P>                <P>                    <inputtype= "Submit"name= "Btnsubmit"ID= "Btnsubmit"value= "Submit"class= "BTN">                    <inputtype= "Reset"name= "Btnreset"ID= "Btnreset"value= "Reset"class= "BTN">                </P>            </form>

Dom (ix) Setting a text box with the DOM

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.