Lock all checkboxes in a column

In response to the question at http://stackoverflow.com/questions/15086000/how-to-disable-all-checkbox-within-the-column-when-button-is-clicked/

by Barney Carroll

HTML

<table id="key_table" border="3">
	<thead>
		<tr>
			<th>
				Month 1<button id="1" class="lockButton">Lock</button><br>2013-01-01 to 2013-01-31<br>
				<table style="width: 250px;">
					<tbody>
						<tr class="odd">
							<td style="text-align:center;">1</td>
							<td style="text-align:center;">2</td>
							<td style="text-align:center;">3</td>
							<td style="text-align:center;">4</td>
						</tr>
					</tbody>
				</table>
			</th>
			<th>
				Month 2 <button id="2" class="lockButton">Lock</button><br>2013-02-01 to 2013-02-28<br>
				<table style="width: 250px;">
					<tbody>
						<tr class="odd">
							<td style="text-align:center;">1</td>
							<td style="text-align:center;">2</td>
							<td style="text-align:center;">3</td>
							<td style="text-align:center;">4</td>
						</tr>
					</tbody>
				</table>
			</th>
		</tr>
	</thead>
	<tbody>
		<tr class="odd">
			<td style="width: 250px;">
				<table>
					<tbody>
						<tr class="odd">
							<td style="text-align:center;width:100px !important;"><input style="width: 15px !important;" class="kw_460853mo_1" type="checkbox"></td>
							<td style="text-align:center;width:100px !important;"><input style="width: 15px !important;" class="kw_460853mo_1" type="checkbox"></td>
							<td style="text-align:center;width:100px !important;"><input checked="checked" style="width: 15px !important;" class="kw_460853mo_1" type="checkbox"></td>
							<td style="text-align:center;width:100px !important;"><input style="width: 15px !important;" class="kw_460853mo_1" type="checkbox"></td>
						</tr>
					</tbody>
				</table>
			</td>
			<td style="width: 250px;">
				<table>
					<tbody>
						<tr class="odd">
							<td style="text-align:center;width:100px !important;"><input style="width: 15px !important;" class="kw_460853mo_2" type="checkbox"></td>
							<td style="text-align:center;width:100px !important;"><input style="width: 15px !important;" class="kw_460853mo_2" type="checkbox"></td>
							<td...

JavaScript

$(function lockTrigger(){
	$('.lockButton').each(function setUpToggleFunction(){
		// Keep a record of whether checkboxes are currently locked or not
		var locked = false;
		// Which column are we in?
		var column      = $(this).closest('th').index();
		// Grab all the checkboxes in that column - we need to increase column by 1, because CSS counts differently to Javascript (starting with 1 rather than 0)
		var $checkboxes = $('#key_table > tbody > tr > td:nth-child(' + (column + 1) + ')').find('[type=checkbox]');

		$(this).on('click', function toggleLock(e){
			e.preventDefault();

			// Make locked true if it was false, or vice-versa
			locked = !locked;

			// And apply that value to the 'disabled' attribute of the checkboxes
			$checkboxes.attr('disabled', locked);
		});
	});
});