Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8051121
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T07:14:09+00:00 2026-06-05T07:14:09+00:00

I would like two things to happen. One when a selection is made the

  • 0

I would like two things to happen.

One

when a selection is made the input field updates as the total,

Two

the check boxes are dynamically generated and may sometimes be up to
six and sometimes as low as one product.

This is what I have made, but I can’t get it to work 🙁

   <script type="text/javascript">
    function updateTotal(){
        document.getElementById('total').value = 
            (document.getElementById('date0').checked?
             parseFloat(document.getElementById('date0').price):0) +
             (document.getElementById('date1').checked?
             parseFloat(document.getElementById('date1').price):0) +
             (document.getElementById('date2').checked?
             parseFloat(document.getElementById('date2').price):0);
    }
    </script>

With this as form

<form>
 <td id="datecontainer" onchange="Process(this.options[this.selectedIndex].value)">
   <input id="date0" type="checkbox" name="form[date]" value="blue" price="10" onChange="javascript:updateTotal();">product 1<br />
   <input id="date1" type="checkbox" name="form[date]" value="green" price="30" onChange="javascript:updateTotal();">product 2<br />
   <input id="date2" type="checkbox" name="form[date]" value="red" price="50" onChange="javascript:updateTotal();">product 3<br />
 </td>
 <td>
  Total cost is:
  <input name="total" id="total" type="text" readonly="readonly" style="border:0px;">
 </td>
</form>

Any help will be great!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T07:14:10+00:00Added an answer on June 5, 2026 at 7:14 am

    You can do this by changing your code and markup as follows:

    HTML

    <td id="datecontainer" onchange="Process(this.options[this.selectedIndex].value)">
       <input id="date0" type="checkbox" name="form[date]" value="blue" data-price="10" onChange="updateTotal();">product 1<br />
       <input id="date1" type="checkbox" name="form[date]" value="green" data-price="30" onChange="updateTotal();">product 2<br />
       <input id="date2" type="checkbox" name="form[date]" value="red" data-price="50" onChange="updateTotal();">product 3<br />
     </td>
     <td>
      Total cost is:
      <input name="total" id="total" type="text" readonly="readonly" style="border:0px;">
     </td>​
    

    JS

    function updateTotal(){
        var date0 = document.getElementById('date0');
        var date1 = document.getElementById('date1');
        var date2 = document.getElementById('date2');
        var amount = 0;    
        amount += date0.checked ? parseFloat(date0.getAttribute('data-price')) : 0;
        amount += date1.checked ? parseFloat(date1.getAttribute('data-price')) : 0;
        amount += date2.checked ? parseFloat(date2.getAttribute('data-price')) : 0;
        document.getElementById('total').value = amount;
    
    }​
    

    A couple of things to note:

    You do not need to include the javascript: before the function in the onchanged attribute.
    You should prefix custom attributes with the data-* prefix.
    You should surround your td tags with tr and table. The way that you are using them it would make more send to use an ul tag with li children.

    DEMO – http://jsfiddle.net/9mxh5/

    If the checkboxes are dynamically generated and you don’t know how many you may have:

    HTML

    <td id="datecontainer" onchange="Process(this.options[this.selectedIndex].value)">
       <input id="date0" type="checkbox" name="form[date]" value="blue" data-price="10" onChange="updateTotal(this);">product 1<br />
       <input id="date1" type="checkbox" name="form[date]" value="green" data-price="30" onChange="updateTotal(this);">product 2<br />
       <input id="date2" type="checkbox" name="form[date]" value="red" data-price="50" onChange="updateTotal(this);">product 3<br />
     </td>
     <td>
    

    JS

    var amount = 0;
    
    function updateTotal(element){
            var price = parseFloat(element.getAttribute('data-price'));
            amount += element.checked ? price : price*(-1);
            document.getElementById('total').value = amount;
    
        }​
    

    DEMO – http://jsfiddle.net/9mxh5/2/

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to do two things within my Android app : Check if
I would like to have two things happen at once when I click on
I would like to accomplish two things during my build process: Run unit tests
I would like to be able to merge two files into one during configure
Two things can happen: old data is overwritten with new one with some round-robin
I would like my two applications to be able to send strings to each
I would like a two-column web page with the right panel fixed-width and the
I have a problem i would like parallelize two for loops with openmp. how
I would like to display two values in an drop down view of my
I would like to make a LaTeX document with two tables of contents, like:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.