473,788 Members | 2,814 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Adding values from radio button & select box

5 New Member
Hi, firstly I am a total freshie in all this. From what I have gathered on the web and this forum, I finally managed to get my form up.

I have a set of radio buttons with values to it and a select box with values too. Depending on the options selected from the two, the values will be calculated and displayed. everything worked fine with the calculation and im getting the right amount totalled up.

I only have one issue, whenever the radio button is clicked, the values gets updated but when a different option is selected from the select box it doesnt gets updated unless i click once more on the radio button.

Below is my code. Any kind of help is much appreciated. Thanks!

Expand|Select|Wrap|Line Numbers
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  2. <html xmlns="http://www.w3.org/1999/xhtml">
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
  5. <title>Untitled Document</title>
  6. <style type="text/css">
  7. <!--
  8. .style1 {
  9.     font-size: 18px;
  10.     font-family: Arial, Helvetica, sans-serif;
  11. }
  12. .style8 {font-size: 11px; font-family: Arial, Helvetica, sans-serif; }
  13. .style9 {font-size: 18px; font-family: Arial, Helvetica, sans-serif; color: #FF0000; }
  14. .style10 {
  15.     color: #CCCCCC
  16. }
  17. -->
  18. </style>
  19. <script type="text/javascript"> 
  20.     function checkIt(el) { 
  21.         var adType;
  22.         if (el.value == 'hot_banner'){
  23.             adType = 500
  24.         }else if(el.value == 'hip_banner'){
  25.             adType = 300
  26.         }else if(el.value == 'cool_banner'){
  27.             adType = 150
  28.         }else if(el.value == 'top_text'){
  29.             adType = 100
  30.         }else if(el.value == 'tower_text'){
  31.             adType = 80
  32.         }else if(el.value == 'center_text'){
  33.             adType = 150
  34.         }
  35.  
  36.         var mths; 
  37.         mths = document.getElementById("duration").value;
  38.         document.getElementById("amount").value=parseInt(adType)*parseInt(mths);
  39.     }
  40. </script>
  41. </head>
  42.  
  43. <body>
  44. <table width="605" border="0" cellpadding="5" cellspacing="0" bgcolor="#F7F7F7">
  45.   <tr>
  46.     <td width="197" align="right" valign="top" class="style8">Select Advertising Location:</td>
  47.     <td width="388" class="style8"><label>
  48.       <input type="radio" name="type" id="hot_banner" value="hot_banner" onclick="checkIt(this)"/>
  49.       Hot Cover Banner Package (S$500/month) <a href="#">See Location</a><br />
  50.       <input type="radio" name="type" id="hip_banner" value="hip_banner" onclick="checkIt(this)"/>
  51.       Hip Horizontal Banner Package (S$300/month) <a href="#">See Location</a><br />
  52.       <input type="radio" name="type" id="cool_banner" value="cool_banner" onclick="checkIt(this)"/>
  53.       Cool Tower Banner (Big) Package (S$150/month) <a href="#">See Location</a><br />
  54.       <input type="radio" name="type" id="top_text" value="top_text" onclick="checkIt(this)"/>
  55.       Happening Top Text Package (S$100/month) <a href="#">See Location</a><br />
  56.       <input type="radio" name="type" id="tower_text" value="tower_text" onclick="checkIt(this)"/>
  57.       Happening Tower Text Package (S$80/month) <a href="#">See Location</a><br />
  58.       <input type="radio" name="type" id="center_text" value="center_text" onclick="checkIt(this)"/>
  59.       Happening Center Text Package (S$150/month) <a href="#">See Location</a></label></td>
  60.   </tr>
  61.   <tr>
  62.     <td align="right" class="style8">Select Advertisment Duration:</td>
  63.     <td class="style8"><p>
  64.       <select name="duration" id="duration">
  65.         <option onchange="checkIt(this)" value="1" selected="selected">1 Month</option>
  66.         <option onchange="checkIt(this)" value="2">2 Months</option>
  67.         <option onchange="checkIt(this)" value="3">3 Months</option>
  68.         <option onchange="checkIt(this)" value="4">4 Months</option>
  69.         <option onchange="checkIt(this)" value="5">5 Months</option>
  70.         <option onchange="checkIt(this)" value="6">6 Months</option>
  71.         <option onchange="checkIt(this)" value="12">12 Months</option>
  72.       </select>
  73.     </p>
  74. </td>
  75.   </tr>
  76.   <tr>
  77.     <td colspan="2" align="center" class="style8"><span class="style9">S$<b>
  78.     <input name="amount" type="text" id="amount" size="12" value="auto calculated" readonly="readonly"/>
  79.     </b></span></td>
  80.   </tr>
  81. </table>
  82. </body>
  83. </html>
Jan 9 '08 #1
8 2981
acoder
16,027 Recognized Expert Moderator MVP
onchange should be added to the select element, not to each option.
Jan 9 '08 #2
crayfiss
5 New Member
Ahh.. thats where i went wrong. That really does the trick. Thanks so much.

But now, after selecting from the select box, I got a NaN but after clicking back on the radio button it outputs back the right calculation.
Jan 9 '08 #3
acoder
16,027 Recognized Expert Moderator MVP
That's because you're using the same checkIt() function and passing it the select element as the argument (this) whereas it expects a radio button. What you should do is change checkIt to have no arguments (don't pass 'this' to it) and find the selected radio button using the checked property. Loop over the radio buttons until you find the checked one.
Jan 9 '08 #4
crayfiss
5 New Member
Hi acoder, once again thanks for pointing out my mistakes. I've tried to find out ways to do this but no matter what I tried I got myself lost further.

Could you assist me one last time by showing me how this can be done from amending my code. Thanks again.
Jan 9 '08 #5
crayfiss
5 New Member
Hi can anyone else help a newbie on this issue. I need to see a sample code. Thanks
Jan 10 '08 #6
acoder
16,027 Recognized Expert Moderator MVP
Something like this would give you the checked radio button:
Expand|Select|Wrap|Line Numbers
  1. var checkedRadio;
  2. var radios = document.getElementsByName("type");
  3. for (var i = 0; i < radios.length; i++) {
  4.   if (radios[i].checked) {
  5.     checkedRadio = radios[i];
  6.   }
  7. }
Jan 10 '08 #7
crayfiss
5 New Member
Thank you so much for the help acoder!!
Jan 10 '08 #8
acoder
16,027 Recognized Expert Moderator MVP
You're most welcome. Glad you got it working. Post again if you have any more questions.
Jan 10 '08 #9

Sign in to post your reply or Sign up for a free account.

Similar topics

4
2577
by: cwwilly | last post by:
Hello, Thanks for taking a look at this! Problem: I'm trying to pass multiple dynamic values between a slaveform and a masterform. The problem I'm having is on the slaveform I loop through multiple records and want two values depending on the row they select. slaveform: x=selected
1
4255
by: hortoristic | last post by:
We are using JavaScript to Enable/Disable certain fields on web pages based on business rules. A simple example is if when using an option type tag, and the two options are Yes and No. If YES is selected - enable a field to use the M$ Datepicker. Using the code below works for most of our fields, however the problem is that when the field is re-enabled - it remembers the original date or data prior to it being disabled - despite the...
32
2172
by: Ed Jay | last post by:
A complete js newbie is asking this question: I have a form comprised of several questions, each answered with a radio button. I'd like to use onClick to bring up additional text at the end of the form (not in a text box). I've tried using document.write("yada"), but it clears the page before showing the text. How can I do it? BTW, I'm currently using js in conjunction with CSS to unhide the text. This works fine, except it requires...
1
2979
by: Joe Attardi | last post by:
Hi all, On a form on one of my pages I have two <select> elements, and each one is paired up with a radio button. The idea is to choose an item from one list or the other and select the radio button of the list you want to use. I'm using JavaScript to automatically select the radio button corresponding to a list when you click on the list (using the onclick event handler). Is that the best event to change on, or should this
6
4649
by: Ian Davies | last post by:
Hi me again, sorry to be a pain. Ive been struggling with this one all day. Hope you can understand whats happening. First my script is below. Have a look and I'll explain at the bottom what it does so far and is failing to do ******************************* <?php session_start(); header("Cache-control: private"); //IE 6 Fix include("myconn.php");
2
12443
by: Eric Layman | last post by:
Hi, I have a radio button and a combo box. Upon changing a value for radio button, the combo box values will be dynamically updated. I have an idea on how to go abt doing it but Im stuck into converting into code.
1
1702
by: Sideswipe | last post by:
I am trying to concatenate selected values from numerous radio button sets into 1 series and assign it to a single hidden field. I have never done javascript before and I continue to run into problem after problem (object has no properties, undefined functions, etc.) So, I am really hoping someone can end the madness for me. Just so it's understood, the id's and names of the radio buttons vary with a row in a table so I CANT know the...
0
1287
by: Jimstershat | last post by:
Hello Everyone, I need to do the following; I need to create the following url using a radio form: http://www.domain.com/addtocart.aspx?productid=18&variantid=37&quantity=1 I need to be able to let the customer choose different product ids that always have a variant id associated with each product id. In other words If customer chooses product 1 it will always have a variantid= 10 and product 2 will always have variantid=20. So .. I...
6
2076
by: dba | last post by:
using the following code with a problem.... echo "<input type='hidden' name='member_id' value=\"{$row}\">{$row}"; echo "<input type='radio' name='member_name' value=\"{$row}\">{$row}<br />"; The post_data.php program posts the following member id is: 0009
0
9656
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10366
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10173
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10110
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
6750
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5399
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5536
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4070
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2894
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.