Add checkboxes together.

Discussion in 'JavaScript' started by Creations, Mar 2, 2008.

  1. #1
    Hello Everyone,

    I am trying to create checkboxes that whenever someone clicked on it has a value and that value adds it to the total. I have this so far just don't know where to go from there.

    
    <html>
    
    	<head>
    	
    	<script language="javascript">
    	
    	function addition(param) {
    	
    	
    	
    	
    	}
    	
    	
    	</script>
    	
    	</head>
    
    	<body>
    		
    		<h1> Estimate Calculator</h1>
    		
    		<form name="estimatecal">
    		
    			First Name: <input type="text" /> <br />
    			Last Name: <input type="text" /> <br />
    			URL: <input type="text" /> <br />
    		
    			<h4>Web Design</h4>
    			<input type="checkbox" value="200" onclick="addition(this)"/>200 dollars<br />
    			<input type="checkbox" value="100" onclick="addition(this)"/>100 dollars<br />
    			<input type="checkbox" value="500" onclick="addition(this)"/>500 dollars<br />
    			</hr>	
    			
    			<h4>Graphic Design</h4>
    			<input type="checkbox" value="100" onclick="addition(this)"/>150 dollars<br />
    			<input type="checkbox" value="50" onclick="addition(this)" />50 dollars<br />
    			</hr>
    			
    			Total: 
    			
    		</form>
    		
    	
    	</body>
    
    </html>
    
    
    Code (markup):

     
    Creations, Mar 2, 2008 IP
  2. Morishani

    Morishani Peon

    Messages:
    239
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Try this script :
    <script language="javascript">
    var sum = 0;
    
    function addition(param)
    	{
    	if (param.checked == true)
    		sum += parseInt(param.value);
    	else
    		sum += -parseInt(param.value);
    
    	docoument.getElementById("totalSpan").innerText = sum;
    	}
    </script>
    HTML:
    And put this span after "Total : "
    <span id="totalSpan">0</span>
    HTML:
    Good luck, I hope it works.
     
    Morishani, Mar 3, 2008 IP