JS Code Synchronization text box content

Source: Internet
Author: User

HTML code:
 
----------------------
 
<Html>
 
<Head>
 
<Script>
// Synchronous Function
Function synchronize (){
Document. getElementById ('i1'). value = document. getElementById ('i2 '). value;
// Alert ("synchronization successful ");
}

// Execute Synchronization
SetInterval (synchronize, 500); // synchronization interval, which is synchronized every 0.5 seconds
</Script>
</Head>
<Body>
If you enter a character in the second input box, it is automatically synchronized to the first input box. <Br/>
The first input box: <input type = "text" size = "45" id = "i1" name = "first"> <br/>
The second input box: <input type = "text" size = "45" id = "i2" name = "second">
</Body>
</Html>

Save the preceding HTML code as an html file and open it in a browser to see the effect. Note: Some browsers prohibit local scripts for security reasons. Click "run.
In the process of searching data, we find that not only the change event can be handled, but other events can also be handled. For example, keyup events. Here is another example:
Www.2cto.com
// This is not a real-time change
<Input type = "text" id = "t1" value = "" size = "45" onchange = "document. getElementById ('t2 '). value = this. value "/>
<Input type = "text" id = "t2" value = "" size = "45" onchange = "document. getElementById ('t1 '). value = this. value "/>
 

 
// This is a real-time change, but it won't be detected if you use the mouse to mark it, so you can combine the two.
<Input type = "text" id = "t3" value = "" size = "45" onkeyup = "document. getElementById ('t4 '). value = this. value "/>
<Input type = "text" id = "t4" value = "" size = "45" onkeyup = "document. getElementById ('t3 '). value = this. value "/>

Author: Heng_Ji

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.