The function is based in java, however it uses HTML, I really believe this is the best place for this post, if not, I know it would go under the HTML section and I'm sorry for posting this here. Basically, I'm trying to calculate a total that a user has generated by entering in a number, having that multiplied by 15, and adding it to a value determined by the selection of two radio buttons, one that has a value of 40 and another that has a value of 0. I've done all I can and am stumped at this point. Here's the code: <HTML> <HEAD> <TITLE>Simple Calculator</TITLE> <SCRIPT LANGUAGE="JavaScript"> function CalculateSum(Atext, Btext, Ctext, form) { var A = parseInt(Atext); var B = parseInt(Btext); form.Answer.value = 15*A + B; } function ClearForm(form) { form.input_A.value = ""; form.input_B.value = ""; form.Answer.value = ""; } </SCRIPT> </HEAD> <BODY> <FORM NAME="Calculator" METHOD="post"> <P>Number to be multiplied by 15: <INPUT TYPE=TEXT NAME="input_A" SIZE=10></P> <P>Radio buttons:<br> <input type="radio" name="input_B" value=40 /> Button with value of 40<br> <input type="radio" name="input_B" value=0 /> Button with value of 0 <P><INPUT TYPE="button" VALUE="Total" name="AddButton" onClick="CalculateSum(this.form.input_A.value, this.form.input_B.value, this.form)"></P> <P><INPUT TYPE="button" VALUE="Clear Fields" name="ClearButton" onClick="ClearForm(this.form)"></P> <P>Answer = <INPUT TYPE=TEXT NAME="Answer" SIZE=12></P> </FORM> </BODY> </HTML> Code (markup):
The main problem here is that a bit more JavaScript is needed to find out which one of the radio buttons is checked. Here it is, redone and working. Note that I have changed the arguements passed in the CalculateSum function so they are not the values but the element objects, because of the extra js needed later in the case of the radio buttons. Also, note the use of the reset element - it saves having to set up your own ClearForm function, if you are just wanting to clear all the form inputs simultaneously. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html lang="en"> <HEAD> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <TITLE>Simple Calculator</TITLE> <SCRIPT type="text/javascript"> function CalculateSum(Atext, Bradio, form) { var A = parseInt(Atext.value); for (var i=0; i < Bradio.length; i++) { // find which one of the radio buttons is selected if (Bradio[i].checked) { var B = parseInt(Bradio[i].value); } } form.Answer.value = 15*A + B; } </SCRIPT> </HEAD> <BODY> <FORM NAME="Calculator" METHOD="post" action=""> <P>Number to be multiplied by 15: <INPUT TYPE="TEXT" NAME="input_A" SIZE="10"> </P> <P>Radio buttons:<br> <input type="radio" name="input_B" value="40"> Button with value of 40<br> <input type="radio" name="input_B" value="0"> Button with value of 0 </p> <P><INPUT TYPE="button" VALUE="Total" name="AddButton" onclick="CalculateSum(this.form.input_A, this.form.input_B, this.form)"> </P> <p><input type="reset" value="Clear Fields"></p> <P>Answer = <INPUT TYPE="TEXT" NAME="Answer" SIZE="12"></P> </FORM> </BODY> Code (markup): </HTML>
Awesome! That's exactly what I needed! I figured it needed an if statement, but it's been so long since I've coded Java that I wasn't really sure about it, much less how to do it. Thank you so much!
What would I have to add to make the additional value of 40 go to 80 when they select the radio button for 40, if the number they input is over 15? I know it would have to be an if statement, but I'm not very good at that so I'm lost. That sounds confusing, but really I just want them to input a value that will be multiplied by 15, if they select the radio button with forty, I want the total to be their value (multiplied by 15) plus the forty, however if they input a value greater than or equal to 15, I want the buttons value to be 80. If they select the value with zero, I want the total to be just the value they put in multiplied by 15, without any additional add-ons, regardless if the value they've entered is over 15. Sorry if that's not clear.
If I've got that right, you want B to be 80 when the following 2 conditions are satisfied: 1. the user has input a value of 15 or more into input 'A', and also 2. the user has selected the '40' radio button, but otherwise you want B to be the default value from that selected radio button If so, just add the highlighted line: function CalculateSum(Atext, Bradio, form) { var A = parseInt(Atext.value); for (var i=0; i < Bradio.length; i++) { // find which one of the radio buttons is selected if (Bradio[i].checked) { var B = parseInt(Bradio[i].value); } } [color=blue]if (A >= 15 && B == 40) B = B * 2;[/color] form.Answer.value = 15*A + B; } Code (markup):
That's exactly what I needed! Thank you so much! I've been to other forums in the past with questions like this, and instead of actually being helped, I just get trash for asking simple questions. Definitely one of the best forums I've ever been to.