Click here to Skip to main content
15,896,201 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I got a problem with the Javascript Onclick event handler, i haven click the radio button and press the submit button, it direct show me the total cost. May i know how to solve it

and the second problem is i want to made the total cost show in alert form. May i know how??
this is my coding
XML
<?xml version = "1.0" encoding = "utf-8" ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
      http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns = "http://www.w3.org/1999/xhtml">
   <head><title> Practical Lab 5.5</title>
   <script type = "text/javascript" src = "sum.js"></script></head>
   <body>
   <form name = "myForm" action = "" >
	Motherboard (RM59 each) <input type="radio" id="value1" name="value1" onclick ="addNumbers(val1)" value="59" />
        <br />
        Harddisk (RM49 each) <input type="radio" id="value2" name="value2" onclick ="addNumbers(val2)" value="49" />
        <br />
        Keyboard (RM39 each) <input type="radio" id="value3" name="value3" onclick ="addNumbers(val3)" value="39" />
        <br />
        <input type="button" name="Submit" value="Submit" onclick="javascript:addNumbers()"/>
        <br />
        Total cost including 5% tax = <input type="text" id="answer" name="answer" value=""/>
      	</form>
   	</body>
	</html>


JavaScript
function addNumbers()
                {
                  	var val1 = parseInt(document.getElementById("value1").value);
                        var val2 = parseInt(document.getElementById("value2").value);
			var val3 = parseInt(document.getElementById("value3").value);
                        var ansD = document.getElementById("answer");
                        ansD.value = val1 + val2 + val3 + 7.35;
                }
Posted
Updated 3-May-12 2:48am
v2
Comments
Sandeep Mewara 3-May-12 8:49am    
Not clear on what is the issue and where are you stuck.
Jayfam 3-May-12 8:51am    
normally is i click the radio button on the 3 product, then only press submit to get the total cost
but this case is i direct press submit button, it comes out the total cost. so this is my problem

1 solution

hi,

just some idea

why do you use
HTML
<input type="text" id="answer" name="answer" value="" />

you could use
HTML
<span id="answer"></span>

and
JavaScript
document.getElementById("answer").innerHTML = val1 + val2 + val3 + 7.35;


i'm not a js guru, but sometimes i use "number" function
example i'd try
JavaScript
document.getElementById("answer").value = Number(val1) + Number(val2) + Number(val3) + 7.35;
 
Share this answer
 
v4

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900