Onpropertychange Event oninput_ form effects compatible with Firefox and IE

Source: Internet
Author: User
When you enter data in a text box, you can use onkeyup to detect events when the keyboard is pressed and released, Onpropertychange can capture each change, and onchange need to execute an event to catch it. But sometimes we enter data in the form of a paste rather than a keyboard input, which requires real-time detection of changes in the state of the text box.
Onpropertychange is not supported by Firefox, if you want to use the normal Firefox, you need to use Oninput properties, and need to register events with AddEventListener.
<div id= "msg" ></div> <input id= ' txt ' value= ' "/> <script>//functions performed when state changes handle () { document.getElementById (' msg '). innerhtml= ' The length of the text entered is: ' +document.getelementbyid (' txt '). value.length; //firefox detection state changes can only be used oninput, and need to use AddEventListener to register events. if (/msie/i.test (navigator.useragent))//ie Browser {document.getElementById (' txt '). Onpropertychange=handle} else {//non IE browsers, such as Firefox document.getElementById (' txt '). AddEventListener ("input", handle,false); } </script>
[Ctrl + A All SELECT Note: If the need to introduce external JS need to refresh to perform]

Example:
Copy Code code as follows:

<body>
<div>oninput Test </div>
<div id= "Testdiv" ><input id= ' tx1 ' name= ' tx1 ' value= ' "/></div>"
</body>
<script language= "JavaScript" >
<!--
function Getos () {//Determine browser type
var osobject = "";
if (Navigator.userAgent.indexOf ("MSIE") >0) {
return "MSIE";
}
if (Isfirefox=navigator.useragent.indexof ("Firefox") >0) {
return "Firefox";
}
if (Issafari=navigator.useragent.indexof ("Safari") >0) {
return "Safari";
}
if (Iscamino=navigator.useragent.indexof ("Camino") >0) {
return "Camino";
}
if (Ismozilla=navigator.useragent.indexof ("gecko/") >0) {
return "Gecko";
}

}

if (Navigator.userAgent.indexOf ("MSIE") >0) {
document.getElementById (' tx1 '). attachevent ("Onpropertychange", Txchange);
}else if (navigator.userAgent.indexOf ("Firefox") >0) {
document.getElementById (' tx1 '). AddEventListener ("input", txchange2,false);
}
function Txchange () {
Alert ("TestIE");
}
function TxChange2 () {
Alert ("Testfirefox");
}
</script>

The above is the compatible Firefox Onpropertychange event method.

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.