1

I am writing a function that will be triggered every time I release the mouse(mouseup) from a inside a table row. This is accomplished using jquery: $('textarea').mouseup(function (e){ ...code }); Everything works fine except for when I add a new row to the table. Once I add the new row, i seems that the mouseup event is not detected. See the code in the link and do the following: 1. Write some text in the Col2 or Col3 2. Highlight the text. Expected: An alert will be shown with the text. 3. Press Add Row button, write something and highlight the text. The expected alert is not shown.

JsFiddle

Thanks!

sanoj lawrence
  • 951
  • 5
  • 29
  • 69
Joel
  • 336
  • 3
  • 14
  • try to use .on('mouseup') – Mohamed-Yousef Dec 29 '14 at 23:07
  • Please don't include links in code comments. Include the code as the warning when you entered that link told you to do. Also, this is a duplicate of [this question](http://stackoverflow.com/questions/16062899/jquery-doesnt-work-after-content-is-loaded-via-ajax). Don't worry about the fact that the new HTML is loaded via AJAX; it's the same problem. – Heretic Monkey Dec 29 '14 at 23:08

3 Answers3

2

Use this $(document).on('mouseup',selector,function(){});

  • I think it's a good idea to make the event delegation as specific as possible, rather than arbitrarily binding to the `document`. In this example, if there is another ` – AlexZ Dec 29 '14 at 23:21
  • i didn't read the part that said "inside a table" so you are correct – Seif El Deen Khaled Dec 29 '14 at 23:29
1

You can delegate events in jQuery. How I'd write your javascript.

$('table').on('mouseup', 'textarea', function({
  console.log('I work!');
});

Here is a working update of your jsFiddle

AlexZ
  • 11,515
  • 3
  • 28
  • 42
  • Another question. I have a function to add more rows to the table. Once information has been entered in the textarea fields, I want to clone the table and replace every – Joel Jan 05 '15 at 16:03
  • @Joel, can you pleas make a new question (with a jsFiddle) and post a link? I think this comment area isn't enough space to provide a proper answer. – AlexZ Jan 06 '15 at 01:16
0

you can try this...

$(document).on('mouseup','textarea',function(){
});

This would help you. keep coding :)

Amrinder Singh
  • 82
  • 1
  • 1
  • 10