0

I am using this for catch paste evente on a textarea :

$('textarea#id').on('paste', function (event) {
   alert('paste !');
});

It work fine on the page where the texteara is but it doesnt work if textarea#id is not already on the page, if I call it in ajax (like if its inside a remote modal).

Tahola
  • 1,040
  • 3
  • 19
  • 38
  • Possible duplicate of [Jquery event handler not working on dynamic content](http://stackoverflow.com/questions/15090942/jquery-event-handler-not-working-on-dynamic-content) – JP. Aulet Feb 04 '17 at 11:39
  • You can take a look at this posts: http://stackoverflow.com/questions/15090942/jquery-event-handler-not-working-on-dynamic-content http://stackoverflow.com/questions/203198/event-binding-on-dynamically-created-elements In order to know how to attach events to dynamic elements (you should point the event to an existing parent). Hope this helps. – JP. Aulet Feb 04 '17 at 11:40

1 Answers1

2

Change Script like this

$(function(){
$(document).on('paste', '#textArea', function () {
   alert('paste !');
});
});
<html>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<!DOCTYPE html>

<body>

<textarea rows="4" cols="50" id="textArea">

</textarea>

</body>
</html>

Note :- The .on() method attaches event handlers to the currently selected set of elements in the jQuery object.

Aman Kumar
  • 4,533
  • 3
  • 18
  • 40