Jump to content

how to get multiple checkbox values in javascript


smartzain

Recommended Posts

<script type="text/javascript">function total_amount(){document.getElementById("grand_total").innerHTML = "";var sugar_check = 100;var milk_check = 200;var curd_check = 50;var rice_check = 250;var grand_total = "";var amount = 0;if(document.getElementById('sugar_check').checked == true){amount = sugar_check;}if(document.getElementById('milk_check').checked == true){amount = milk_check;}if(document.getElementById('curd_check').checked == true){amount = curd_check;}if(document.getElementById('rice_check').checked == true){amount = rice_check;}else{alert('Please make a selection first...! ');}document.getElementById("grand_total").innerHTML = amount;}</script>

<div id="text">  <form name="myForm" action="" method="post">    <ul>	  <li>	    <input type="checkbox" name="item" id="sugar_check" />	    Sugar = Rs.100</li>	  <li>	    <input type="checkbox" name="item" id="milk_check" />	    Milk = 200</li>	  <li>	    <input type="checkbox" name="item" id="curd_check" />	    Curd = 50</li>	  <li>	    <input type="checkbox" name="item" id="rice_check" />	    Rice = 250</li>    </ul>    		<a href="javascript:;" onclick="total_amount()">SUBMIT</a>  </form></div>

Link to comment
Share on other sites

You haven't told us what you're trying to do. Seeing that you have four checkboxes, it seems that you're only printing the result corresponding to the last checkbox that is checked in the case that more than one checkbox is checked. Every time you write "amount = value" you're overwriting the current value with a new one.

Link to comment
Share on other sites

Addition is done with the + operator. If grand_total is meant to be a numeric sum then you should initialize it as 0. How familiar are you with Javascript? This is pretty basic.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...