0

I wrote this code

   $(document).on("click", "#tasksTable tr", function(e) {
       var taskId = this.id;
       var chkBoxCompleted = $(this).find('input:checkbox:first');
       var chkBoxNotRequired = $(this).find('input:checkbox:last');

       chkBoxCompleted.on('click', function(){
           alert("called when I click second time on checkbox")
       });

       chkBoxNotRequired .on('click', function(){
           alert("called when I click second time on checkbox")
       });
    });

and my html is like

<table width="100%"  cellpadding="0" cellspacing="0" style="border-width: 0px 1px 1px 0px;" id="tasksTable">
   <tr id="9">
   <td class="blueFont_inset"><span>22/05/2014 00:48</span>&nbsp;</td>
   <td class="blueFont_inset"><input type="checkbox" name="completedTask[]"  id="completedTask" value="N"><span>&nbsp;</span></td>
   <td class="blueFont_inset"><span>Completed Date</span>&nbsp;</td>
   <td class="blueFont_inset"><input type="checkbox" name="notRequiredTask[]" id="notRequiredTask" value="N"><span>&nbsp;</span></td>
  </tr>
</table>

These are multiple rows inside this table.

Click event of checkbox called when I click any of the checkbox second time. I tried it in IE and Chrome. Is this because I am using it inside table row click event? But I want to use that table row click event too.

I am using

  <script src="//code.jquery.com/jquery-1.10.2.js"></script>
2
  • 3
    Event handlers inside event handlers will do that, and it's not even the right event handlers, you should be using the change event, and not nest them. Commented May 28, 2014 at 14:34
  • Um, every time you click on the row, you will be adding clicks, that is a bad idea. Commented May 28, 2014 at 14:39

2 Answers 2

1

Your first click event is being intercepted by the click listener on the TR. Rather than using a click binding to bind events to the inner elements, I would bind it all on document.ready and make all references relative to the clicked elements parent scope.

Sign up to request clarification or add additional context in comments.

7 Comments

Can you please elaborate it how I can write them separately.. thanks
Change the ID's to classes (ie: completedTask), and bind the events directly to them. If you need the row ID, inside of your click event callbacks, find $(this).parents('tr').attr('id') to get it.
It looks like he is already using delegate syntax on the parent row binding
you mean I should write it like '$(document).on("click", "#tasksTable tr .completedTask", function(e) ' if I change id to classes. it still not working
I don't need rows. Rows are already there. I just displayed one row to save space.
|
0

Try to remove check box click handlers before bind again otherwise click event is binding again and again while click event is propagated to tr.

Ref: http://api.jquery.com/off/

Better avoid binding event handlers with in parent event handler.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.