Click here to Skip to main content
15,895,656 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
May i know where is my error because its seen like not working
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">

<!-- Pratical Lab 5.5-->

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


and this is my javascript
JavaScript
function addNumbers()
                {
                  	var val1 = parseInt(document.getElementById("value1").59);
                        var val2 = parseInt(document.getElementById("value2").49);
			var val3 = parseInt(document.getElementById("value3").39);
                        var ansD = document.getElementById("answer");
                        ansD.value = val1 + val2 + val3;
                }
Posted
Updated 17-Apr-18 6:08am
v2
Comments
Sergey Alexandrovich Kryukov 2-May-12 15:58pm    
Reason for my vote of 1
Makes no sense whatsoever.
--SA

The whole idea is wrong: the radio buttons do not carry numeric values. You need to explain why would you need something like what you do, in terms of application.

The attribute onclick is designed for a click event handler, not numeric data. The JavaScript code already contains numbers 59, 49, 39, so it would not serve any purpose even if it worked. I don't think further analysis makes any sense.

—SA
 
Share this answer
 
It throws the error 'Uncaught SyntaxError: Unexpected number sum.js:3'. Because document.getElementById("value1") should use one of the Element property. Instead your code has the improper numbers like 59, 49 and 39.

What do you want to achieve there?
 
Share this answer
 
Comments
Jayfam 2-May-12 15:15pm    
im just want to add the total price of the product 59 + 49 +39 by selecting the radio button
I had correct the error and made somethings like this, but the click vent handler not work for me, i haven choose the radio button and click submit, it show the output to me
HTML
     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="Sumbit" value="Submit" onclick="javascript:addNumbers()"/>
        <br />
        Total cost including 5% tax = <input type="text" id="answer" name="answer" value=""/>
      	</form>
   	</body>


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;
                }
 
Share this answer
 
Please get the correct code of ur issue. Let me know if you need more information on it.

XML
<head runat="server">
    <title></title>
    <script type="text/javascript">

        function addNumbers()
        {
            var val1 = parseInt((document.getElementById("value1").value) + ".59");
            var val2 = parseInt((document.getElementById("value2").value) + ".49");
            var val3 = parseInt((document.getElementById("value3").value) + ".39");
            var ansD = document.getElementById("answer");
            ansD.value = val1 + val2 + val3;
        }
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        Motherboard (RM59 each)
        <input type="radio" id="value1" name="value1" value="59" />
        <br />
        Harddisk (RM49 each)
        <input type="radio" id="value2" name="value2" value="49" />
        <br />
        Keyboard (RM39 each)
        <input type="radio" id="value3" name="value3" value="30" />
        <br />
        <input type="button" name="Sumbit" value="Submit" onclick="javascript:addNumbers()" />
        <br />
        Total including 5% sales tax=
        <input type="text" id="answer" name="answer" value="" />
    </div>
    </form>
</body>
 
Share this answer
 
Comments
Jayfam 3-May-12 3:59am    
its still not work, haven click the radio button, and press submit then auto show total cost already. It is not logic right?

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