Real-time listening input box value change: Oninput & Onpropertychange

Source: Internet
Author: User

In Web development often encounter the need to dynamically monitor input box value changes, if you use OnKeyDown, onkeypress, onkeyup this several keyboard events to monitor, can not listen to the right button copy, clip and paste these operations, Working with combination shortcuts is also cumbersome. So this article introduces you to a perfect solution: listen to input box value changes in conjunction with HTML5 standard event oninput and IE exclusive event Onpropertychange events.

Oninput is a standard event for HTML5 , which is useful for detecting textarea, Input:text, Input:password, and input:search elements that have changed through the user interface. Triggered immediately after the content has been modified, unlike the onchange event that needs to lose focus. The compatibility of Oninput events in mainstream browsers is as follows:

  

As can be seen from the table above, theoninput event is not supported in the following versions of IE9 and requires the use of IE-specific Onpropertychange event substitution, which is triggered when the user interface changes or the content is modified directly using a script. There are several situations:

    • Modified the Input:checkbox or Input:radio element selection state, the checked attribute changed.
    • The value of the Input:text or TEXTAREA element has been modified, and the property of value has changed.
    • The selected item of the SELECT element has been modified, and the SelectedIndex property has changed.

After you hear the onpropertychange event, you can use the PropertyName property of the event to get the name of the property that has changed.

The sample code for collection Oninput & onpropertychange monitoring input box content changes is as follows:

123456789101112131415161718     <script type="text/javascript">    // Firefox, Google Chrome, Opera, Safari, Internet Explorer from version 9        function OnInput (event) {            alert ("The new content: "+ event.target.value);        }    // Internet Explorer        functionOnPropChanged (event) {            if(event.propertyName.toLowerCase () == "value") {                alert ("The new content: "+ event.srcElement.value);            }        }    </script><body>    Please modify the contents of the text field.    <input type="text"oninput="OnInput (event)" onpropertychange="OnPropChanged (event)"value="Text field"/></body>

With the jQuery Library, you only need to bind Oninput and Onpropertychange two events at the same time, the sample code is as follows:

123 $(‘textarea‘).bind(‘input propertychange‘function() {    $(‘.msg‘).html($(this).val().length + ‘ characters‘);});

  oninput and Onpropertychange These two events in IE9 have a small bug, that is, through the right-click menu menu cut and Delete commands to delete the content will not be triggered, and the other version of IE is normal, There is no good solution at the moment. However, Oninput & Onpropertychange is still the best solution for monitoring input box value changes.

Real-time listening input box value change: Oninput & Onpropertychange

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.