The HTML structure will help a ton, but the first optimization is to cache your first checkbox. Then we use the supposed iteration in jQuery. If I understand what you are trying to do, I get the following:
$('.mutuallyexclusive').live("click", function() { var $check = $(this); var checkedState = $check.attr('checked'); $('.mutuallyexclusive:checked') .attr('checked', '') .attr('name', 'chk'); $check.attr('checked', checkedState); if (checkedState) { $check.attr('name', $check.prev('select').val()); } else { $check.attr('name', 'dd'); } });
I couldn’t say exactly from your question whether you want to assign a value to the checkbox in the selection list or vice versa. I went with "set the checkbox name to the value of the select list". Hope you were after.
source share