简体   繁体   中英

jquery on input paste in contenteditable div

There is a contenteditable div as a textarea in my html page:

<div id="txt" contenteditable="true"></div>

I detect the user's input through jquery "input paste"

$('#txt').on('input paste',function(event){
    alert("ok"); 
});

It works fine in chrome, but not working in IE. Could anyone tell me what happened? Thanks~~

IE is returning event.type as paste

$('#txt').on('input paste',function(event){
   if(event.type=='paste' || event.type=='input'){
       alert("ok"); 
   }  
});

EDIT. For some reason I only tested with paste.

$('#txt').on('keyup paste',function(event){
   alert("ok"); 
});

I think this is a problem which is IE specific. Alternate solution is to use focus, keyup event and blur event. You can compare old and new text to see if change is there in text or not.

Just found a SO thread where given answer lined up with my answer; and a link to original SO thread.

$('#txt').on('focus', function() {
  before = $(this).html();
}).on('blur keyup paste', function() { 
  if (before != $(this).html()) { $(this).trigger('change'); }
});

$('#txt').on('change', function() {alert('changed')});

SO Thread

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM