Checkbox value

Hi,

I am trying to update the textarea with the checkboxvalues.

when checkboxa is checked,

checkbox1, checkbox2, checkbox3 are checked and their values should be shown in textarea like 1,2,3

if checkbox1 is unchecked the textarea should show 2,3

same thing applies to checkbox2 and checkbox3

below is the code where i attempted to do it.

Can someone tell me how to do it

<script language=“JavaScript” type=“text/javascript” src=“jquery/jquery.min.js”></script>

<input type=“checkbox” class=“checkboxa”>
<input type=“checkbox” class=“checkbox1” value=“1”>
<input type=“checkbox” class=“checkbox1” value=“2”>
<input type=“checkbox” class=“checkbox1” value=“3”>

<input type=“checkbox” class=“checkboxb”>
<input type=“checkbox” class=“checkbox2” value=“4”>
<input type=“checkbox” class=“checkbox2” value=“5”>
<input type=“checkbox” class=“checkbox2” value=“6”>

<textarea rows=“6” cols=“100” id=“checkboxvalue”> </textarea>

<script language=“JavaScript” type=“text/javascript”>
<!–
$(‘.checkboxa’).click(function(){
var status = this.checked;
var allvals = ;
$(‘.checkbox1’).each(function(){
this.checked = status;

		allvals.push(this.val());
})	

$("#checkboxvalue").text(allvals);

});

$(‘.checkboxb’).click(function(){
var status = this.checked;
$(‘.checkbox2’).each(function(){
this.checked = status;
})
});

//–>
</script>

Thanks

How about if the checkboxa and checkboxb contained info stating what they relate to?


<input type="checkbox" class="checkboxa" rel="checkbox1">
...
<input type="checkbox" class="checkboxb" rel="checkbox2">

Then you could have just the one click event, that uses the value in the rel attribute to control the others.


$('.checkboxa, .checkboxb').click(function () {
    var checkbox = this;
    $('.' + $(this).attr('rel')).each(function () {
        this.checked = checkbox.checked;
    });
});

Not sure why you would need this but give this a try (probably can be done alot better…) :

Test HTML

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.min.js" type="text/javascript"></script>
<form>
	<label>Box A</label>
	<input type="checkbox" name="checkboxa" /><br />
		<input type="checkbox" class="checkbox1" value="1" /><br />
		<input type="checkbox" class="checkbox1" value="2" /><br />
		<input type="checkbox" class="checkbox1" value="3" /><br />

	<label>Box B</label>
	<input type="checkbox" name="checkboxb"><br />
		<input type="checkbox" class="checkbox2" value="4"><br />
		<input type="checkbox" class="checkbox2" value="5"><br />
		<input type="checkbox" class="checkbox2" value="6"><br />

	<textarea rows="6" cols="100" id="checkboxvalue"></textarea>
</form>

Test JS:


<script language="JavaScript" type="text/javascript">
	function addToTextArea( cls ) {
		var array = [];
		$(cls).change(function() {
			if( $(this).is(':checked') ) {
				array.push(
					$(this).val()
				);
			} else {
				array.splice( 
					jQuery.inArray(
						$(this).val(),
						array
					), 
					1 
				);		
			}
			$('#checkboxvalue').val( 
				array.join(',') 
			);	
		});	
	}

	function checkAll( el, cls ) {
		$(el).click(function() {
			if( $(this).is(':checked') ) {
				$(cls).each(function() {
					$(this).attr('checked', 'checked');
					$(this).trigger('change');
				});	
			} else {
				$(cls).each(function() {
					$(this).removeAttr('checked');
					$(this).trigger('change');					
				});		
			}

		});
	}		

	addToTextArea('.checkbox1, .checkbox2');
	checkAll('input[name="checkboxa"]', '.checkbox1');
	checkAll('input[name="checkboxb"]', '.checkbox2');
</script>