SitePoint Sponsor

User Tag List

Results 1 to 3 of 3

Thread: checkbox value

  1. #1
    SitePoint Enthusiast
    Join Date
    Feb 2007
    Posts
    60
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    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

  2. #2
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,718
    Mentioned
    103 Post(s)
    Tagged
    4 Thread(s)
    How about if the checkboxa and checkboxb contained info stating what they relate to?

    Code html4strict:
    <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.

    Code javascript:
    $('.checkboxa, .checkboxb').click(function () {
        var checkbox = this;
        $('.' + $(this).attr('rel')).each(function () {
            this.checked = checkbox.checked;
        });
    });
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  3. #3
    SitePoint Addict
    Join Date
    Dec 2005
    Posts
    336
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Not sure why you would need this but give this a try (probably can be done alot better....) :

    Test HTML
    Code HTML4Strict:
    <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:
    Code JavaScript:
    <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>


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •