Skip to main content

How do I iterate the price in parentheses and add it to the total price, My code doesn't seem to work

I've found this on google and gave it a go. But it seems as if its not doing anything. it isn't updating the price total area at all, nor is it adding or subtracting.

<script type="text/javascript">
    function doMath() {
        // Capture the entered values of two input boxes
        var my_input1 = document.getElementsByName("case").value;
        var my_input2 = document.getElementsByName("cpu").value;
        var my_input3 = document.getElementsByName("cooling").value;
        var my_input4 = document.getElementsByName("ram").value;
        var my_input5 = document.getElementsByName("gpuchoice").value;
        var my_input6 = document.getElementsByName("storage").value;
        var my_input7 = document.getElementsByName("storage1").value;
        var my_input8 = document.getElementsByName("storage2").value;
        var my_input9 = document.getElementsByName("powersupchoice").value;
        var my_input10 = document.getElementsByName("fans").value;
        var my_input11 = document.getElementsByName("lighting").value;
        var my_input12 = document.getElementsByName("sleeving").value;
        var my_input13 = document.getElementsByName("peripherals").value;
        var my_input14 = document.getElementsByName("headsets").value;
        var my_input15 = document.getElementsByName("controllers").value;
        var my_input16 = document.getElementsByName("wirelessadapter").value;
        // Add them together and display
        var sum = parseInt(my_input1) + parseInt(my_input2) + parseInt(my_input3) + parseInt(my_input4) + parseInt(my_input5) + parseInt(my_input6) + parseInt(my_input7) + parseInt(my_input8) + parseInt(my_input9) + parseInt(my_input10) + parseInt(my_input11) + parseInt(my_input12) + parseInt(my_input13) + parseInt(my_input14) + parseInt(my_input15) + parseInt(my_input16);
      document.getElementsByName("text2").value = sum;
      document.getElementsByName("text2").innerHTML = sum;
    }
      </script>

the specific page im trying to get it to work on is: voidtechpcs.com/testbuild.php It's not pretty so be warned, I'm fairly new to this.

Any advice, or a better way of handling this perhaps?

The code works, but it wont get the data price of storage 1 or storage 2, with those two removed from the sum function it adds the totals of everything else perfect. just not these two inputs.

<select name="storage1" id="storage1">
               <option data-productprice="45.00" value="None" selected>None</option>
               <option data-productprice="45.00" value="Crucial MX500 SSD 500 GB (+$0.00)">Crucial MX500 500 GB (+$0.00)</option>
               <option data-productprice="45.00" value="Crucial MX500 SSD 1 TB (+$0.00)">Crucial MX500 1 TB (+$0.00)</option>
               <option data-productprice="45.00" value="Crucial MX500 SSD 2 TB (+$0.00)">Crucial MX500 2 TB (+$0.00)</option>
             </select>

here is the JS that is broken with the storage options in sum function, remove the parseFloat(my_input7) everything works great. put it back in and NAN haha

function doMath() {
  //var regExp = /\(([^)]+)\)/;
  //var matches = regExp.exec("I expect five hundred dollars ($500).");

  // Capture the entered values of two input boxes
  var my_input1 = document.querySelector('[name=case]:checked').dataset.productprice;
  var my_input2 = document.querySelector('[name=cpu]:checked').dataset.productprice;
  var my_input3 = document.querySelector('[name=cooling]:checked').dataset.productprice;
  var my_input4 = document.querySelector('[name=ram]:checked').dataset.productprice;
  var my_input5 = document.querySelector('[name=gpuchoice]:checked').dataset.productprice;
  var my_input6 = document.querySelector('[name=storage]:checked').dataset.productprice;
  var my_input7 = document.querySelector('[name=storage1]').dataset.productprice;
  var my_input8 = document.querySelector('[name=storage2]').dataset.productprice;
  var my_input9 = document.querySelector('[name=powersupchoice]:checked').dataset.productprice;
  var my_input10 = document.querySelector('[name=fans]:checked').dataset.productprice;
  var my_input11 = document.querySelector('[name=lighting]:checked').dataset.productprice;
  var my_input12 = document.querySelector('[name=sleeving]').dataset.productprice;
  var my_input13 = document.querySelector('[name=peripherals]').dataset.productprice;
  var my_input14 = document.querySelector('[name=headsets]').dataset.productprice;
  var my_input15 = document.querySelector('[name=controllers]').dataset.productprice;
  var my_input16 = document.querySelector('[name=wirelessadapter]').dataset.productprice;
  // Add them together and display
  var sum =  parseFloat(55.00) + parseFloat(50.25) + parseFloat(20.55) + parseFloat(my_input1) + parseFloat(my_input2) + parseFloat(my_input3) + parseFloat(my_input4) + parseFloat(my_input4) + parseFloat(my_input6) + parseFloat(my_input7);
  
  document.getElementById('pricetotal').value = sum;
  document.getElementById('text2').innerHTML = sum;
}
Via Active questions tagged javascript - Stack Overflow https://ift.tt/8q9zHVK

Comments

Popular posts from this blog

Prop `className` did not match in next js app

I have written a sample code ( Github Link here ). this is a simple next js app, but giving me error when I refresh the page. This seems to be the common problem and I tried the fix provided in the internet but does not seem to fix my issue. The error is Warning: Prop className did not match. Server: "MuiBox-root MuiBox-root-1" Client: "MuiBox-root MuiBox-root-2". Did changes for _document.js, modified _app.js as mentioned in official website and solutions in stackoverflow. but nothing seems to work. Could someone take a look and help me whats wrong with the code? Via Active questions tagged javascript - Stack Overflow https://ift.tt/2FdjaAW

How to show number of registered users in Laravel based on usertype?

i'm trying to display data from the database in the admin dashboard i used this: <?php use Illuminate\Support\Facades\DB; $users = DB::table('users')->count(); echo $users; ?> and i have successfully get the correct data from the database but what if i want to display a specific data for example in this user table there is "usertype" that specify if the user is normal user or admin i want to user the same code above but to display a specific usertype i tried this: <?php use Illuminate\Support\Facades\DB; $users = DB::table('users')->count()->WHERE usertype =admin; echo $users; ?> but it didn't work, what am i doing wrong? source https://stackoverflow.com/questions/68199726/how-to-show-number-of-registered-users-in-laravel-based-on-usertype

Why is my reports service not connecting?

I am trying to pull some data from a Postgres database using Node.js and node-postures but I can't figure out why my service isn't connecting. my routes/index.js file: const express = require('express'); const router = express.Router(); const ordersCountController = require('../controllers/ordersCountController'); const ordersController = require('../controllers/ordersController'); const weeklyReportsController = require('../controllers/weeklyReportsController'); router.get('/orders_count', ordersCountController); router.get('/orders', ordersController); router.get('/weekly_reports', weeklyReportsController); module.exports = router; My controllers/weeklyReportsController.js file: const weeklyReportsService = require('../services/weeklyReportsService'); const weeklyReportsController = async (req, res) => { try { const data = await weeklyReportsService; res.json({data}) console