Jump to content

how to get multiple checkbox values in javascript


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 post
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 post
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 post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...