how to bind input and td together into a single click event?
how to bind input and td together into a single click event?
Hi, I have got a checkbox in each cell, and I only want to show the cell as selected when the checkbox is clicked.
Here is the code I've written:
[code]
$('#example td input').live('click', function() {
$('#example td').live( 'click', function() {
if ($(this).hasClass('cell_selected') )
$(this).removeClass('cell_selected');
else
$(this).addClass('cell_selected');
} );
} );
[/code]
The problem is it takes 2 click events show the cell as selected, so it is unsuccessful.
Would like to know how to improve it.
Cheers.
Here is the code I've written:
[code]
$('#example td input').live('click', function() {
$('#example td').live( 'click', function() {
if ($(this).hasClass('cell_selected') )
$(this).removeClass('cell_selected');
else
$(this).addClass('cell_selected');
} );
} );
[/code]
The problem is it takes 2 click events show the cell as selected, so it is unsuccessful.
Would like to know how to improve it.
Cheers.
This discussion has been closed.
Replies
[code]
$('#example td input').live('click', function() {
$(this).parent().trigger("click");
} );
$('#example td').live( 'click', function() {
if ($(this).hasClass('cell_selected') )
$(this).removeClass('cell_selected');
else
$(this).addClass('cell_selected');
} );
[/code]
[code]$('#example td,input').live('click', function() {
$(this).parent().trigger("click");
} ); [/code]
note the addition of the comma between 'td' and 'input'.
[code]
$('#example td input').live('click', function() {
if ($(this).parent().hasClass('cell_selected') )
$(this).parent().removeClass('cell_selected');
else
$(this).parent().addClass('cell_selected');
} );
[/code]
Cheers!!
[code]
$('#example td input').live('click', function() {
$(this).parent().toggleClass('cell_selected');
} );
[/code]