3

I am trying to make it so when they click on a <tr> it will open up a dialog which works just fine. However within this <tr> I have an input check box that I need to allow them to click on without opening up the dialog. How is this possible?

$('#messageContainer tr').click(function () {
    var masterMessageID = $(this).attr('messageID');
    var replyToID = $(this).attr('replyToID');

    buildDialog(this.id, masterMessageID, replyToID);
    $(this).removeClass('messageNew');
});

HTML:

<tr replytoid="3" messageid="7078" id="16">
    <td>&nbsp;&nbsp;<input type="checkbox" name="checkAll"></td>
    <td>John Doe</td>
    <td>sdfsdf</td>
    <td>3/14/2012 1:29:47 PM</td>
</tr>

4 Answers 4

6

Stop the propagation of the click:

$('#messageContainer tr :checkbox').click(function(e){
  e.stopPropagation();
});
Sign up to request clarification or add additional context in comments.

3 Comments

Will it work for bubbling down events? Wouldn't the tr click be called first if the events bubble down?
@SKS Events bubble up, not down.
@RichardD I think you should check quirksmode.org/js/events_order.html
1

You can add this code:

$('#messageContainer tr input:checkbox').click(function (e) {
    e.stopPropagation();
});

This will stop the click on the checkbox from propagating up to the tr.

Example - http://jsfiddle.net/U5LBR/1/

Comments

0

Check this

Add event.stopPropagation() in checkbox. It will stop the event bubbling.

$('#messageContainer tr').click(function () {
$('#popup').toggle();
});

$('#messageContainer input[type="checkbox"]').click(function(e){
alert('Checked');
 e.stopPropagation();

})

​

http://jsfiddle.net/JLDzq/1/

2 Comments

I am not too familiar with bubbling. Would the first answer the one that just adds 'e.stopPropagation();' be good or bad?
First answer is doing the same.
0

Edit: Changed from .is(':checkbox') to check for e.target.type

DEMO

$('#messageContainer tr').click(function (e) {
    if (e.target.type && e.target.type == 'checkbox') {
       e.stopPropagation(); //stop bubbling
       return;
    }

    var masterMessageID = $(this).attr('messageID');
    var replyToID = $(this).attr('replyToID');

    buildDialog(this.id, masterMessageID, replyToID);
    $(this).removeClass('messageNew');
});

4 Comments

@Alnitak I tried .not() but could not get it to work correctly.
@JamesWilson I think Guffa has a better solution. In case if you don't want an additional handler, then see my edits above.
@SKS Alright, I tried Guffa's and it seems to work. I haven't tested it in any old old browser. But it seems to do the trick for me in current versions of FF and IE.
@JamesWilson the propagation was the real fault, I was just proposing .not() instead of !(...).is()

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.