Totaling values from multiple selection boxes in javascript

James Curran 26 Reputation points
2022-11-09T10:50:11.68+00:00

Hi all, I believe this to be a simple problem, but it has had me struggling for a while. I have posted 3 different ways I have tried to solve it, but to no avail. Basically, I have filled a table with selection boxes based on how much data I have in a database. I would like to do a number of things with the data that the user has selected in the selection boxes. The first task I would like to do is total the individual values. I will later use individual values that I have selected for some analysis.

The problem I am having is that I cannot read what has been selected. I can either read the value of the first selection box only or I can get the values of all the options in all of the drop down boxes. I have tried lots of variations. I will code dump

<!DOCTYPE html>
<html lang="en">
<head>
<title>Beverages</title>ghjgh
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<link
href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css"
rel="stylesheet"
/>
<link rel="stylesheet" href="stylesheets/style.css">
<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Lato">
<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Montserrat">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
</head>
<body>
<!-- Navbar -->
<div class="wa-top">

<div class="wa-bar wa-blue wa-card wa-right-align wa-large">
<a class="wa-bar-item wa-button wa-hide-medium wa-hide-large wa-right wa-padding-large wa-hover-white wa-large wa-blue" href="javascript:void(0);" onclick="myFunction()" title="Toggle Navigation Menu"><i class="fa fa-bars"></i></a>
<a href="#" class="wa-bar-item wa-button wa-padding-large wa-white">Home</a>
<a href="#" class="wa-bar-item wa-button wa-hide-small wa-padding-large wa-hover-white">Beverages</a>
<a href="#" class="wa-bar-item wa-button wa-hide-small wa-padding-large wa-hover-white">Link 2</a>
<a href="#" class="wa-bar-item wa-button wa-hide-small wa-padding-large wa-hover-white">Link 3</a>
<a href="#" class="wa-bar-item wa-button wa-hide-small wa-padding-large wa-hover-white">Link 4</a>
<div class="logo"><a href="../home.html" class="wa-logo"><img src="images/17GLogo.png" alt="17g Webapp" /></a></div>
<!-- Navbar on small screens -->
<div id="navDemo" class="wa-bar-block wa-white wa-hide wa-hide-large wa-hide-medium wa-large">
<a href="#" class="wa-bar-item wa-button wa-padding-large">Link 1</a>
<a href="#" class="wa-bar-item wa-button wa-padding-large">Link 2</a>
<a href="#" class="wa-bar-item wa-button wa-padding-large">Link 3</a>
<a href="#" class="wa-bar-item wa-button wa-padding-large">Link 4</a>
</div>
</div>
<div class="container mt-5">
<% if (messages.success) { %>
<p class="alert alert-success mt-4"><%- messages.success %></p>
<% } %>
<br />
<table class="table">
<thead>
<tr>
<th scope="col">#Id</th>
<th scope="col">Name</th>
<th scope="col">Price</th>
<th scope="col">Quantity</th>
<th scope="col">Milk</th>
</tr>
</thead>
<tbody>
<% if(data.length){ for(var i = 0; i< data.length; i++) {%>
<tr>
<th scope="row"><%= (i+1) %></th>
<td><%= data[i].name%></td>
<td>$ <%= data[i].price%></td>

         <td>  
      <form action="insertlabel.php" id="privtelabel" method="POST">  

          <label for="quantity" ></label>  
          <select id="quantity1">   
            <option value="0">0</option>  
            <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>    
          </select>  
          <td>  
   <select name='milk' id='milk'>  
    <option value="Regular Milk">Regular Milk</option>  
    <option value="Oat Milk">Oat Milk</option>  
    <option value="Light Milk">Light Milk</option>  
    </select>  
  </td>  
</form>  
        </td>  
        </tr>  
        <% } }else{ %>  
        <tr>  
          <td>No record found</td>  
        </tr>  
        <% } %>  
        <tr>  
          <td>Total Price: <span id="totalCost"></span></td>  
        </tr>  
      </tbody>  
        
    </table>  
     <!-- <script>  

/* function myFunction() {
var x = document.getElementById("btn");
x.disabled = true;
} */
$(document).ready(function(){

        total_cost = 0;  
        alert(total_cost)  
          $('#quantity option').each(function(){  
             total_cost = total_cost + parseInt($(this).val())  
          })  
         alert(total_cost)  
      }) -->      
  <head>  
      

    <script>  

function addExtrasCost()
{
var total_cost = 2;
alert(total_cost);
oSels = document.getElementById('quantity').getElementsByTagName("quantity");
alert(oSels);
for (i=0; i< oSels.length; i++){

  alert(i);  
    
}  

}  

function getI(){
total_cost = 0;

    $('#quantity1').each(function(){  
       total_cost = total_cost + parseInt($(this).val());          
    })  
    alert(total_cost);  
}  

function getJ(){  
  total_cost = 0;    
  $('#quantity1').each(function(index,value){        
       total_cost = total_cost + parseInt($(this).val());    
       alert(value.value);  
    })  
    alert(total_cost);  
}  

function getK(){  
  total_cost = "";  
    selects = document.querySelectorAll("form");  
    alert(selects.length)  
      for( let i = 0; i < selects.length; i++){  
        num = selects[i].value;  
        total_cost = total_cost + num;  
          
      }  
      alert(total_cost);  
}  
    </script>  
    </head>  
    <body>    
    <button onclick ="getK()">Order</button>   
    </body>  
  </div>  

</body>
</html>

Developer technologies ASP.NET Other
0 comments No comments
{count} vote

Accepted answer
  1. Lan Huang-MSFT 30,186 Reputation points Microsoft External Staff
    2022-11-15T09:49:29.237+00:00

    Hi @James Curran ,
    Sorry, apparently I misunderstood your layout.
    You change

    const collection = document.forms['privtelelabel'].getElementsByTagName('select');  
    

    to

    const collection = document.getElementsByTagName('select');  
    

    Best regards,
    Lan Huang


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.