Biker John
Biker John

Reputation: 2711

Trigger event on click except for checkbox + label

I have a table row, which triggers an event when clicked. There are (not displayed) checkbox + its styled label inside the row.

What i want is to prevent (i guess with :not or .not() but cant figure it out) the execution if checkbox/label is clicked.

HTML:

<center>
<table>
    <tr class='pend'>
        <td><input type="checkbox" id="bb"/> <label for="bb">X</label></td>
        <td>&nbsp;</td>
        <td>some text</td>
    </tr>
</table>
</center>

CSS:

center {
margin-top:20px;
}

input[type=checkbox] + label {
    width:10px;
    height:10px;
    background-color:red;
}

input[type=checkbox] {
    display:none;
}

table tr {
height:40px;
    background-color:gray;
}

table td {
padding:5px;
}

JS:

$('.pend').on('click',function(){
    $(this).append('text');
    return false;
})

JSFIDDLE: http://jsfiddle.net/ySuGB/2/

Upvotes: 0

Views: 1526

Answers (4)

dyurkavets
dyurkavets

Reputation: 436

$('.pend').on('click',function(){
    var $this = $(this);
    $this.off('click');
    $this.find('td:eq(2)').append('text');
})​

Upvotes: 0

Korvin Szanto
Korvin Szanto

Reputation: 4501

e.stopPropagation() is what you need.

http://jsfiddle.net/jNDYJ/

$('.pend input:checkbox').add('.pend label[for=bb]').click(function(e){
    e.stopPropagation();
});

Upvotes: 1

Jonathan
Jonathan

Reputation: 5018

You can check if event target is label (etc):

$('.pend').on('click',function(e){
  if(e.target.tagName=='LABEL')
    return false;
  $(this).append('text');
  return false;
})​

http://jsfiddle.net/ySuGB/9/

Upvotes: 0

Selvakumar Arumugam
Selvakumar Arumugam

Reputation: 79830

You need to prevent event bubbling on click of checkbox/label.

See below,

//                     V-- Is the TD containing the checkbox and the label.
$('.pend :checkbox').parent().on('click', function(event) {
    event.stopPropagation()
});

DEMO: http://jsfiddle.net/ySuGB/12/

Upvotes: 2

Related Questions