Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    New to the CF scene
    Join Date
    Jul 2006
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    i have problem regarding counting checkboxes

    HI everyone,

    In my project i have group of 7 check boxes. Each check box is given a value(as 1,2,3,4,5 etc.). The sum of checkboxes should not be over some value(let us suppose 7). If the user checks on boxes, and if the value is greater than the predetermined value, windows alert has to be there telling that it exceeded the predetermined value.

    can anyone help me in writing javascript for this?????? It will be of great help to me

    ronnie

  • #2
    New Coder
    Join Date
    Nov 2005
    Location
    Central Indiana
    Posts
    51
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    
    <html>
    <head>
    <title>Untitled</title>
    <script type="text/javascript">
    
    var counter = 0;
    
    function checkValue(o) {
    	if (o.checked == true) {
    		if (counter + parseInt(o.value) > 7) {
    			o.checked = false;
    			alert('Over 7');
    		}
    		else {
    			counter = counter + parseInt(o.value);
    		}
    	}
    	else {
    		counter -= parseInt(o.value);
    	}
    	
    	document.getElementById('value').innerText = counter;
    }
    
    </script>
    
    </head>
    
    <body>
    
    <input type="checkbox" value="1" onclick="checkValue(this)">1<br>
    <input type="checkbox" value="2" onclick="checkValue(this)">2<br>
    <input type="checkbox" value="3" onclick="checkValue(this)">3<br>
    <input type="checkbox" value="4" onclick="checkValue(this)">4<br>
    <input type="checkbox" value="5" onclick="checkValue(this)">5<br>
    <input type="checkbox" value="6" onclick="checkValue(this)">6<br>
    <input type="checkbox" value="7" onclick="checkValue(this)">7<br>
    
    Value: <span id="value">0</span>
    
    </body>
    </html>

  • #3
    New Coder
    Join Date
    Nov 2005
    Location
    Central Indiana
    Posts
    51
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Whoops....for cross-browser friendliness, replace:

    document.getElementById('value').innerText = counter;

    with:

    document.getElementById('value').firstChild.nodeValue = counter;

  • #4
    New to the CF scene
    Join Date
    Jul 2006
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks very much......., it works fine . But, if the values of the checkboxes are decimal values(like .75, .25 etc....), can u please tell me how to do that

  • #5
    New to the CF scene
    Join Date
    Jul 2006
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    yeah, i got it i changed parseInt to parseFloat!!!!! thanx

  • #6
    Supreme Master coder! _Aerospace_Eng_'s Avatar
    Join Date
    Dec 2004
    Location
    In a place far, far away...
    Posts
    19,291
    Thanks
    2
    Thanked 1,043 Times in 1,019 Posts
    Thanks for doing his homework assignment for him. Just remember in the end its his grade and when it comes to an exam he isn't going to pass.
    ||||If you are getting paid to do a job, don't ask for help on it!||||


  •  

    Posting Permissions

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