2 Replies - 712 Views - Last Post: 30 April 2013 - 11:51 AM

#1 nakins  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 21-May 09

How to add hidden input dollar amounts then display

Posted 30 April 2013 - 11:34 AM

Hi,

I haven't done any javascript coding in a real long time, and I'm not sure what I'm doing wrong here.

I have like four selectable options, which will be base on a div linked by the div or an image like <a href="#" onclick="dosomething"></a>
Each option will have a price stored in a hidden input.

I'm trying to add dollar amounts that are set into hidden input fields based on selection, such as
<input type="hidden" name="price" id="price" value="1.00">

I have a
<script Language="Javascript">
                    document.write(total.toFixed(2))
               </Script>

where I want to display the total in the html, and I have a
<body onload="totalamt()">


function totalamt(){
    var total = 0;

     var item1 =  document.getElementById('feature1name');
     
     if(item1.value == "on"){
          var price1 = document.getElementById('feature1price').value;
          total  += price1.value;
          }

     var item2 =  document.getElementById('feature2name');

     if(item2.value == "on"){
          var price2 = document.getElementById('feature2price').value;
          total  += price2.value;
          }

     var item3 =  document.getElementById('feature3name');

     if(item3.value == "on"){
          var price3 = document.getElementById('feature3price').value;
          total  += price3.value;
          }

     var item4 =  document.getElementById('feature4name');

     if(item4.value == "on"){
          var price4 = document.getElementById('feature4price').value;
          total  += price4.value;
          }

}


Is This A Good Question/Topic? 0
  • +

Replies To: How to add hidden input dollar amounts then display

#2 andrewsw  Icon User is offline

  • It's just been revoked!
  • member icon

Reputation: 3806
  • View blog
  • Posts: 13,488
  • Joined: 12-December 12

Re: How to add hidden input dollar amounts then display

Posted 30 April 2013 - 11:45 AM

<script Language="Javascript">
                    document.write(total.toFixed(2))
               </Script>

language="Javascript" is obsolete and should be type="text/javascript".

Don't use document.write as it is likely to replace the entire content of the page. Use an HTML element and set its innerHTML to something, or set the value of an input-element.

The value retrieved from inputs is initially a string, so if you want to sum these then you should convert them to a number. You are also using value twice.

total += parseFloat(price1);    // price1 is already the value of the element

This post has been edited by Dormilich: 30 April 2013 - 02:36 PM

Was This Post Helpful? 2
  • +
  • -

#3 nakins  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 21-May 09

Re: How to add hidden input dollar amounts then display

Posted 30 April 2013 - 11:51 AM

Thank you for your help.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1