Perfect solution for js/jquery to listen to changes in input box values in real time: oninput & amp; onpropertychange, jqueryoninput

Source: Internet
Author: User

Perfect solution for js/jquery to monitor changes in input box values in real time: oninput & onpropertychange, jqueryoninput

(1) Let's Talk About jquery and useJQueryLibrary, you only need to bind the oninput and onpropertychange events at the same time. Sample Code:

$('#username').bind('input propertychange', function() {$('#content').html($(this).val().length + ' characters');});

  

(2) for native JS writing, oninput is HTML5The standard event is very useful for detecting content changes that occur through the user interface of textarea, input: text, input: password and input: search elements. It is triggered immediately after the content is modified, unlike the onchange event, it is triggered only when the focus is lost. OninputThe compatibility of events in mainstream browsers is as follows:

As shown in the preceding table,OninputThe event is not supported in IE9 or earlier versions and must be replaced by the onpropertychange event unique to IE. This event is triggered when the user interface is changed or the content is directly modified using a script, there are several situations:

  • The status of the input: checkbox or input: radio element is modified, and the checked attribute is changed.
  • The value Attribute of the input: text or textarea element is changed.
  • The selected items of the select element are modified, and the selectedIndex attribute changes.

When listeningOnpropertychangeAfter the event, you can use the propertyName attribute of the event to obtain the name of the property that has changed.

The sample code of the oninput & onpropertychange collection listening input box content change is as follows:

  

From: http://blog.163.com/lgh_2002/blog/static/44017526201341511112874/

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.