jquery encapsulates custom event--valuechange (Dynamic listener Input,textarea) values before values change

Source: Internet
Author: User

jquery encapsulates custom event--valuechange (Dynamic listener Input,textarea) values before values change

JS Monitoring input Box value of the instant changes online there are a lot of onpropertychange, Oninput, but they are not what I want, I want to be dynamic monitoring Input,textarea and other values before and after the change, Like the Valuechange event in Kissy, when I start typing 1 in the input box, the previous value is undefined, and now the value is 1, and when I enter 2, the previous value is 1, Now the value is 2. And so, I want to get this effect, because if there is this method, then I do input,textarea and other operations, it is very convenient, such as I want to monitor the change of textarea value, if the change of what I should do, if the value has not changed, I do what things, is very convenient to use 。 We can look at the results as follows:

Jsfiddler links are as follows:

To view the effects, please click on me!

In fact, with the next jquery custom event operation, we can look at some of the code in jquery sources as follows:

The Setup method is a register event, teardown is the Delete event method. Not much to say, the direct sticker code is as follows:

JS all the code is as follows:

$.event.special.valuechange ={teardown:function(namespaces) {$ ( This). Unbind ('. Valuechange '); }, Handler:function(e) {$.event.special.valuechange.triggerchanged ($ ( This)); }, add:function(obj) {$ ( This). On (' Keyup.valuechange cut.valuechange paste.valuechange input.valuechange ', Obj.selector, $.event.special.valuechange.handler)}, triggerchanged:function(Element) {varCurrent = Element[0].contenteditable = = = ' true '?element.html (): Element.val (), Previous=typeofElement.data (' previous ') = = = ' undefined '? Element[0].defaultvalue:element.data (' previous ')    if(Current!==Previous) {Element.trigger (' Valuechange ', [Element.data (' previous '))]) Element.data (' Previous ', current)} }}

The page is called as follows:

$ (function  () {      $(function  (e, previous) {          $ (' #output '). Append (' Previous: ' +  previous +  '  --current  : ' + $ (this). Val () + ' <br/> ')        }) 
    

HTML Test Code:

<input id= "text" type= "text"/>
<div id= "Output" ></div>

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.