最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to use "input propertychange" events to capture just copy and paste by mouse - Stack Overflow

programmeradmin3浏览0评论

I want to capture change happened in textarea (keyup, and also copy&past), for keyup option i use :

$("textarea").keyup(function(){
   // ajax call here
});

i added this to capture pasting or cutting by mouse then trigger keyup event on the textarea:

$("textarea").on('input propertychange', function() {
    $(this).trigger(keyup);
});

the problem here is if i press a key in my keyboard , i get 2 ajax calls because the second function capture also keyup event.

Is there a way to prevent $("textarea").on('input propertychange'... from detecting a press key ?

I want to capture change happened in textarea (keyup, and also copy&past), for keyup option i use :

$("textarea").keyup(function(){
   // ajax call here
});

i added this to capture pasting or cutting by mouse then trigger keyup event on the textarea:

$("textarea").on('input propertychange', function() {
    $(this).trigger(keyup);
});

the problem here is if i press a key in my keyboard , i get 2 ajax calls because the second function capture also keyup event.

Is there a way to prevent $("textarea").on('input propertychange'... from detecting a press key ?

Share Improve this question edited Oct 14, 2021 at 11:41 Mosh Feu 29.3k18 gold badges93 silver badges141 bronze badges asked Nov 16, 2013 at 22:14 medBouzidmedBouzid 8,41211 gold badges60 silver badges93 bronze badges 2
  • Why not just use the second function?! – Tamer Shlash Commented Nov 16, 2013 at 22:24
  • @TamerShlash because the second event doesn't work in all browsers – medBouzid Commented Nov 16, 2013 at 22:25
Add a ment  | 

2 Answers 2

Reset to default 8

Why not test this simplification? As I tested your code, without success on detecting keyup in 'input propertychange' event.

You ignore keyup event:

//$("textarea").keyup(function(){
//// ajax call here
//});

And capture only this (do ajax call with this):

$("textarea").on('input propertychange', function() {
  //$(this).trigger(keyup);
  // do ajax call here
});

the latter ignores only some control keys, ie, key without corresponding character input.

after doing some research i found a solution here :

How to run a function once when binding to multiple events that all trigger in Javascript?

i think this is the best solution to prevent calling event twice in my situation

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论