Skip to content

Instantly share code, notes, and snippets.

@czj
Created December 8, 2011 15:16
Show Gist options
  • Save czj/1447253 to your computer and use it in GitHub Desktop.
Save czj/1447253 to your computer and use it in GitHub Desktop.
function init_checkout_selection_table(id) {
var table = $(id);
var clicked_td = null;
// Clicking on the input box changes the row to active
// Add this behaviour + simulate the first click
// Use triggerHandler to only execute the function (the input is already clicked)
table.find('input').on('click', function(event) {
table.find('.active').removeClass('active');
clicked_td = $(this).closest('td');
clicked_td.closest('tr').addClass('active');
}).filter(':checked').triggerHandler('click');
// Clicking on the whole table row clicks the input
// Clicking an input also clicks an the TD that contains it
// We have to prevent this behavior (detect if the current TD doesn't contain a INPUT)
// We cannot use stopPropagation() because event has to propagate up to the <form>
// We have to remember which TD has just been clicked !
table.find('td').click(function(event) {
var td = $(this);
if (!td.is(clicked_td))
td.closest('tr').find('input[type=radio]').click();
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment