6 Replies - 1625 Views - Last Post: 18 March 2013 - 07:41 AM

#1 sadbuddy  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 28
  • Joined: 18-November 12

Javascript : Problems in real-time calculation

Posted 18 March 2013 - 05:24 AM

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta content="text/html; charset=utf-8" http-equiv="Content-Type" />
<title>GPS-ware</title>
<script type="text/javascript">
function check()
{
   var sindex,price,qty,sub1,sub2,total;
   sindex = document.getElementById("product").selectedIndex;
   price = document.getElementById("product").option[sindex].value;
   qty = document.getElementById("qty").selectedIndex;
   sub1 = price*qty;
   sub2 = sub1*0.05;
   document.getElementById("sub1").value=sub1.toFixed(2);
   document.getElementById("sub2").value=sub2.toFixed(2);
   
   sub1 = eval(document.getElementById("sub1").value);	
   sub2 = eval(document.getElementById("sub2").value);
   
   document.getElementById("total").value=(sub1+sub2).toFixed(2);

}
</script>
</head>
<body>
<table border="1">
<tr>
<td>
<span>Product:</span>
<select name="product" id="product" onchange="check();">
<option value="0">Product</option>
<option value="25">Product 25</option>
<option value="30">Product 30</option>
<option value="100">Product 100</option>
<option value="300">Product 300</option>
<option value="800">Product 800</option>
</select>
</td>
<td>
<select name="qty" id="qty" onchange="check();">
<option>Quantity</option>
<option>1</option>
<option>2</option>
<option>3</option>
<option>4</option>
</select>
</td>
<td>
<input type="text" name="sub1" id="sub1" value="0.00" disabled="disabled"/>
</td>
</tr>
<tr>
<td>Tax:</td>
<td><input type="text" name="sub2" id="sub2" value="0.00" disabled="disabled"/></td>
</tr>
<tr>
<td>Total :</td>
<td><input type="text" name="total" id="total" value="0.00" disabled="disabled"/></td>
</tr>
</table>
</body>
</html>




I am unable to get the total of the product when i select the product and quantity. I am not sure where went wrong.

Is This A Good Question/Topic? 0
  • +

Replies To: Javascript : Problems in real-time calculation

#2 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 5960
  • View blog
  • Posts: 23,238
  • Joined: 23-August 08

Re: Javascript : Problems in real-time calculation

Posted 18 March 2013 - 06:06 AM

sub1 = eval(document.getElementById("sub1").value); 


Explain why you are using eval() here.
Was This Post Helpful? 0
  • +
  • -

#3 andrewsw  Icon User is offline

  • Fire giant boob nipple gun!
  • member icon

Reputation: 2890
  • View blog
  • Posts: 9,597
  • Joined: 12-December 12

Re: Javascript : Problems in real-time calculation

Posted 18 March 2013 - 06:13 AM

Yes, there is no need to use eval()- there rarely is and it is a very poor practice.

It is options[someindex] NOT option[someindex]

If you learn to use your browsers' console it will help you to debug your page. There are links in my signature.

This post has been edited by andrewsw: 18 March 2013 - 06:17 AM

Was This Post Helpful? 0
  • +
  • -

#4 sadbuddy  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 28
  • Joined: 18-November 12

Re: Javascript : Problems in real-time calculation

Posted 18 March 2013 - 06:19 AM

I have been told that eval is to round up figures.
Was This Post Helpful? 0
  • +
  • -

#5 andrewsw  Icon User is offline

  • Fire giant boob nipple gun!
  • member icon

Reputation: 2890
  • View blog
  • Posts: 9,597
  • Joined: 12-December 12

Re: Javascript : Problems in real-time calculation

Posted 18 March 2013 - 06:32 AM

View Postsadbuddy, on 18 March 2013 - 06:19 AM, said:

I have been told that eval is to round up figures.

That is not correct, there is a Number.round() function for rounding, or other approaches. eval() evaluates an expression and should be avoided if at all possible.

This post has been edited by andrewsw: 18 March 2013 - 06:32 AM

Was This Post Helpful? 1
  • +
  • -

#6 Dormilich  Icon User is online

  • 痛覚残留
  • member icon

Reputation: 3402
  • View blog
  • Posts: 9,619
  • Joined: 08-June 10

Re: Javascript : Problems in real-time calculation

Posted 18 March 2013 - 06:37 AM

note, if you have a <select> element where you only select a single value (i.e. without the multiple attribute), you can get the selected value directly.
 price = document.getElementById("product").value;

Was This Post Helpful? 0
  • +
  • -

#7 sadbuddy  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 28
  • Joined: 18-November 12

Re: Javascript : Problems in real-time calculation

Posted 18 March 2013 - 07:41 AM

Thanks all. I have finally solved my problem !
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1