473,513 Members | 2,563 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Radio button changing a value

I am trying to change the value of the variable "hard" according to
which radio button is pressed and I am having no joy. Could anyone
help me with this, the problematic section is marked with
***********************, I've included all the code incase that isn't
where the problem is.

Any help would be hugely appreciated.

Mick

<HTML>
<HEAD>
<TITLE>Products List</TITLE>
<SCRIPT LANGUAGE="JavaScript">
var items = new Array(10);
var numitems = 0;
function Item(d,c,q,w,h) {
this.desc = d;
this.price = c;
this.quantity = q;
this.wrap = w;
this.hard = h;
}
function additem(desc,price,quantity,wrap,hard) {
items[++numitems] = new Item(desc,price,quantity,wrap,hard);
displaycart();
}

function removeitem() {
///confirms the removal of the last item
if (confirm("\nAre you sure?\n")) {
(numitems--);
displaycart();
}
}

function displaycart() {
var totalcost=0;
with (parent.cart.document) {
open();
write("<HTML><BODY>");
write("<H1>Shopping Cart</H1><HR>");
if (numitems==0) {
write("No items ordered.");
close();
return;
}
write("<TABLE BORDER=1><FORM NAME='form1'>");
for(i=1;i<=numitems;i++) {
write("<TR><TD>");
//the following input box has been modified so that the
//quantity cannot be altered
write("<INPUT NAME='qty' TYPE='TEXT' onfocus='this.disabled=true'
SIZE=2 VALUE=");
write(items[i].quantity + ">");
write("<TD>" + items[i].desc);
write("<TD>£" + items[i].price);
write("<TD>£" + (items[i].price * items[i].quantity));
write("<TD>back" +(items[i].hard));
///////////////
write("<TR>");
write("<TD bgcolor='silver'>");
write("<TD><p>wrap cost</p>");
write("<TD>HARD " +(items[i].hard));
write("<TD>");
write("<TD>£" + (items[i].wrap));
////////////////
// write("<TD>" + (items[i].picture);
write("</TR>\n");
totalcost += (items[i].price *
items[i].quantity+items[i].wrap);
}
totalcost = Math.floor(totalcost*100) /100;
write("<TR><TD COLSPAN=3><B>Total Cost:</B>");
write("<TD>" + totalcost);
write("</TABLE>");
write("<INPUT TYPE=BUTTON VALUE='Update'");
write("onClick='parent.products.updatecart();'>");
write("<INPUT TYPE=BUTTON VALUE='Complete Order'");
write("onClick='parent.products.complete();'>");
//removeitem button
write("<INPUT TYPE=BUTTON VALUE='Remove last item'");
write("onClick='parent.products.removeitem();'>");

write("</FORM></HTML>");
close();
<!--
//currency button
//<form onClick="currency=getRadioValue(this.op_sys);self. alert('The
conversion rate from Sterling is '+currency );">
//<input type="RADIO" name="op_sys" value="1"> Sterling<br>
//<input type="RADIO" name="op_sys" value="1.8248"> Dollar<br>
//<input type="RADIO" name="op_sys" value="1.3662"> Euro<br>
-->
}
}
function updatecart() {
for (i=1; i<=numitems;i++) {
if (numitems == 1)
items[i].quantity = parent.cart.document.form1.qty.value;
else items[i].quantity = parent.cart.document.form1.qty[i-1].value;
}
displaycart();
}

function complete() {
OrdWin=window.open('cart_complete.htm','OrdWin');
}

function wrapping(selectObject){
var val;
var si=selectObject.selectedIndex;
if (si<0) val=null;
else val=selectObject.options[si].value;
return val;

}
</SCRIPT>
</HEAD>
<BODY onLoad="displaycart();">
<IMG SRC="logo.gif" alt="The Good Sports Guide"><BR>
<H1>Products</H1>
<table border="3">

<th colspan="4">The following products are available online. Click the
ORDER NOW link to order a product.</th>
<tr>
<td>
<P><B>The Good Tennis Guide</B></P>
</td>
<td>

//////////////
<script>
function getRadioValue(radioObject) {
var val = null;

for (var i=0; i<radioObject.length;i++) {
if (radioObject[i].checked) val = radioObject[i].value;
}
return val;
}
</script>


</form>
<B>Price:</B>

</td>

<td>
//////
<p>
<script>
wrap=0;

</script>
<select name="bookwrapping"
onchange="if(this.selectedIndex)wrap=+(this[this.selectedIndex].value);">
<option value="not selected" selected disabled></option>
<option value ="0">none</option>
<option value="2.95">Standard Wrapping</option>
<option value="3.95">Deluxe Wrapping</option>

************************************************** *****************************************
<p>Please enter the type of book that you wish to purchase:</p>

<input type="radio" name="radiobutton" value="0"
onclick"if(this.selectedIndex)hard=+(this[this.selectedIndex].value);"/>
<label>Hard Back</label>
<input type="radio" name="radiobutton" value="3"
onclick"if(this.selectedIndex)hard=+(this[this.selectedIndex].value);"/>

<label>Paper Back</label>

</td>

<td>
<INPUT TYPE="BUTTON" VALUE='Add to Cart'
onClick="javascript:additem('tennis', 5.95,1,wrap,hard);");>
</td>
<td>
<img src="image1.gif" width="50" length="75" alt="The Good Tennis
Guide"></A>
</td>
</tr>
<!--
<tr>
<td>
<P><B>The Good Football Guide</B> </P>
</td>
<td>
<B>Price: £5.95</B>
</td>
<td>
<p>test</p>
</td>
<td>
<INPUT TYPE="BUTTON" VALUE='Add to
Cart'onClick="javascript:additem('footie', 5.95,1);");>
</td>
<td>
<img src="image2.gif" width="50" length="75" alt="The Good
Football Guide"></A>
</td>

</tr>

<tr>
<td>
<P><B>The Good Golf Guide RRP: £5.95</B> </P>
</td>
<td>
<B>Price: £5.95</B>
</td>
<td>
<p>test</p>
</td>
<td>
<INPUT TYPE="BUTTON" VALUE='Add to
Cart'onClick="javascript:additem('golf', 5.95,1);");>
</td>
<td>
<img src="image3.gif" width="50" length="75" alt="The Good Golf
Guide"></A>
</td>
</tr>

-->
</table>

</BODY>
</HTML>
Jul 23 '05 #1
1 3366
MickG wrote:
I am trying to change the value of the variable "hard" according to
which radio button is pressed and I am having no joy. Could anyone
help me with this, the problematic section is marked with
***********************, I've included all the code incase that isn't
where the problem is.

Any help would be hugely appreciated.

Mick

<HTML>
<HEAD>
<TITLE>Products List</TITLE>
<SCRIPT LANGUAGE="JavaScript">
The script language attribute is depreciated, use type

<script type="text/javascript">
[...] }
write("<TABLE BORDER=1><FORM NAME='form1'>");
Quotes around attribute values are not essential under most
conditions, however it is generally a good practice to include
them for all attributes.

write("<TABLE BORDER='1'><FORM NAME='form1'>");

for(i=1;i<=numitems;i++) {
write("<TR><TD>");
//the following input box has been modified so that the
//quantity cannot be altered
write("<INPUT NAME='qty' TYPE='TEXT' onfocus='this.disabled=true'
SIZE=2 VALUE=");
Do not use tabs when posting code. Use 1 or 2 spaces for
indenting and manually wrap code at about 65 characters:

for(i=1;i<=numitems;i++) {
write("<TR><TD>");
//the following input box has been modified so that the
//quantity cannot be altered
write("<INPUT NAME='qty' TYPE='TEXT' "
+ "onfocus='this.disabled=true'> SIZE='2' VALUE=");
write(items[i].quantity + ">");
write("<TD>" + items[i].desc);
write("<TD>£" + items[i].price);
write("<TD>£" + (items[i].price * items[i].quantity));
write("<TD>back" +(items[i].hard));
///////////////
write("<TR>");
write("<TD bgcolor='silver'>");
write("<TD><p>wrap cost</p>");
write("<TD>HARD " +(items[i].hard));
write("<TD>");
write("<TD>£" + (items[i].wrap));
This will generate invalid HTML - where are the closing tags?
It will likely render OK in most browsers though.

[...]
write("</FORM></HTML>");
close();
<!--
These are comment markers for HTML, not JavaScript. Use /* */
for blocks or // for lines.
//currency button
//<form onClick="currency=getRadioValue(this.op_sys);self. alert('The
conversion rate from Sterling is '+currency );">
If this is wrapped in your code, it will cause an error.
//<input type="RADIO" name="op_sys" value="1"> Sterling<br>
//<input type="RADIO" name="op_sys" value="1.8248"> Dollar<br>
//<input type="RADIO" name="op_sys" value="1.3662"> Euro<br>
-->
}
}
function updatecart() {
for (i=1; i<=numitems;i++) {
You have used 'i' in multiple functions as a global but with
local context. This is somewhat dangerous, if it's a local,
keep it local

for (var i=1; i<=numitems;i++) {

if (numitems == 1)
items[i].quantity = parent.cart.document.form1.qty.value;
else items[i].quantity = parent.cart.document.form1.qty[i-1].value;
}
For simpler code maintenance, it is good to include all braces:

if (numitems == 1){
items[i].quantity = parent.cart.document.form1.qty.value;
} else {
items[i].quantity =
parent.cart.document.form1.qty[i-1].value;
}
[...] //////////////
<script>
<script type="text/javascript>
function getRadioValue(radioObject) {
var val = null;

for (var i=0; i<radioObject.length;i++) {
if (radioObject[i].checked) val = radioObject[i].value;
}
return val;
}
The following will be a much faster version of the above:

function getRadioValue(radioObject) {
var i = radioObject.length, val = null;
while (i--) {
if (radioObject[i].checked){
return radioObject[i].value;
}
}
// Should never reach here since one button must always
// be checked, but just in case...
return val;
}
[...]
************************************************** *****************************************
<p>Please enter the type of book that you wish to purchase:</p>

<input type="radio" name="radiobutton" value="0"
onclick"if(this.selectedIndex)hard=+(this[this.selectedIndex].value);"/>
<label>Hard Back</label>
<input type="radio" name="radiobutton" value="3"
onclick"if(this.selectedIndex)hard=+(this[this.selectedIndex].value);"/>
You have syntax errors in your onclick code, and the '/' on you
tag ends is for XHTML, not HTML, so don't use them.

Try:

<input type="radio" name="radiobutton" value="0" onclick="
if(this.selectedIndex) {
hard = +(this[this.selectedIndex].value);
}
">
<label>Hard Back<label>
<input type="radio" name="radiobutton" value="3" onclick=
"if(this.selectedIndex) {
hard = +(this[this.selectedIndex].value);
}
">

<label>Paper Back</label>

</td>

<td>
<INPUT TYPE="BUTTON" VALUE='Add to Cart'
onClick="javascript:additem('tennis', 5.95,1,wrap,hard);");>


javascript pseudo protocol is not required, ditch the extra ';'

onClick="additem('tennis', 5.95,1,wrap,hard);")>
[...]

There may be other errors... see how you go.
--
Rob
Jul 23 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
18494
by: Matt | last post by:
I have radio buttons on a form, and when the user makes the selection, it will trigger the event and send the form to the web server and print the selected value. In Case 1, I just use submit...
3
8735
by: steve | last post by:
Sorry if I have post this two times. Hi all What I'm trying to achieve is: I have a form with two radio buttons, if a visitors check radio button 2 and click submit in the next page radio...
4
3253
by: Oscar Monteiro | last post by:
I Have to sets of Radio buttons like so: <input type="radio" name=p1 value=1> <input type="radio" name=p1 value=2> <input type="radio" name=p1 value=3> <br> <input type="radio" name=p2 value=1>...
3
2452
by: Ferret Face | last post by:
Hello, I have a web page that gets the user to select items from a list of options. This list is a set of independant Radio Buttons. I did not use a Radio Button List because I wanted the...
8
5889
by: David Cameron | last post by:
I noticed that using an HTMLInputRadioButton and specifying a value to be an empty string (""), this is overridden by ASP.Net which set the value of the control to be the same as the ID of the...
9
2220
by: IchBin | last post by:
I can not see what the problem is with this script. I am just trying to set a radio button by calling setCheckedValue('abbr_letter', 'V'). Sorry I am new to javascript. <html> <head> <script...
10
6051
by: IchBin | last post by:
I am trying to set the state of a radio button. I do not see what I am doing wrong. Sorry, I am new at this.. I need another set of eyes to look at this snip of code. I am trying to set the radio...
2
12419
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...
1
2306
by: rjlorenzo | last post by:
Good Day, I just need help on my programming. I would like to try to retrieved/edit the data i've save using radio button and want it to displayed on radio button for editing or changing the...
0
7259
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,...
0
7380
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,...
0
7535
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...
1
7098
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...
1
5085
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3232
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...
0
3221
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1592
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 ...
0
455
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...

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.