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 1 of 1
  1. #1
    New to the CF scene
    Join Date
    Nov 2010
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Getting the SUM of the values in Listbox

    What i would like to happen is when i click the GET TOTAL button, i will be able to get the sum of all the values in the Amount List and display the sum in the Total: text box. Help please

    Code:
    <html><head><title>Charges Calculator</title>
    
    <script language="javascript">
    function addOption_all_list(selectbox){
    removeAllOptions(document.form1.Category);
    addOption(document.form1.Category, "110","110");
    addOption(document.form1.Category, "99.99","99.99");
    addOption(document.form1.Category, "19.99","19.99");
    addOption(document.form1.Category, "8","8");
    addOption(document.form1.Category, "5","5");
    addOption(document.form1.Category, "6","6");
    
    }
    
    function addOption(selectbox,text,value )
    {
    	var optn = document.createElement("OPTION");
    	optn.text = text;
    	optn.value = value;
    	selectbox.options.add(optn);
    }
    
    function removeOption(listbox,i)
    {
    	listbox.remove(i);
    }
    
    function addOption_list(){
    for(i=document.form1.Category.options.length-1;i>=0;i--)	{
    var Category=document.form1.Category;
    if(document.form1.Category[i].selected){
    addOption(document.form1.SubCat, document.form1.Category[i].value, 
    
    document.form1.Category[i].value);
    removeOption(Category,i);
    }
    }
    }
    function move_all_Option(selectbox){
    for(i=document.form1.Category.options.length-1;i>=0;i--)	{
    var Category=document.form1.Category;
    addOption(document.form1.SubCat, document.form1.Category[i].value, 
    
    document.form1.Category[i].value);
    }
    removeAllOptions(document.form1.Category);
    }
    
    
    
    function removeAllOptions(selectbox)
    {
    	var i;
    	for(i=selectbox.options.length-1;i>=0;i--)
    	{
    		//selectbox.options.remove(i);
    		selectbox.remove(i);
    	}
    }
    
    </script>
    
    <script language="javascript" type="text/javascript">
    function addition(){
    a=Number(document.form1.SubCat.value);
    b=Number(document.form1.SubCat.value);
    c=a+b;
    document.form1.total.value=c;
    }
    
    function deleteOption(selectObject,optionRank) {
        if (selectObject.options.length!=0) { selectObject.options[optionRank]=null }
    }
    
    function testDelete() {
        var formObject = document.form1
        if (formObject.SubCat.selectedIndex!=-1) {
            deleteOption(formObject.SubCat,formObject.SubCat.selectedIndex)
        } else {
            alert("Select an option and click Delete")
        }
    }
    </script>
    
    </head>
    
    <body onload="addOption_all_list()"; >
    <form name="form1" action="yourpage.php" method="post">
    <input onclick="addOption_all_list()" ;="" value="Add All" type="button"> 
    Rates:		
    <select name="Category" multiple="multiple" size="7">
    <option value="1">1</option>
    <option value="2">2</option>
    <option value="3">3</option>
    <option value="4">4</option>
    <option value="5">5</option>
    <option value="6">6</option>
    
    </select>
    &nbsp;
    
    <input onclick="addOption_list()" ;="" value="Move" type="button"> 
    <input onclick="move_all_Option()" ;="" value="Move All" type="button">
    Amount List:
    <select id="SubCat" name="SubCat" multiple="multiple" size="7">
    </select>
    <input type="button" value="Delete" onclick="testDelete();">
    <input onclick="removeAllOptions(SubCat)" ;="" value="Reset" type="button">
    <input type="button" value="Get Total" onclick="javascript:addition();"></br></br>
    <br>Total:</br>
    <input type="text" name="total">
    </form>
    </body>
    </html>
    Last edited by mikoto; 05-29-2012 at 04:35 AM. Reason: RESOLVED


 

Posting Permissions

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