473,408 Members | 2,839 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,408 software developers and data experts.

passing checked value of radio button to js function from form submit

The following (likely far from imperfect code), reports a value of NaN
in the j4 display. I suppose the problem is I am not really passing
the "checked" value of the radio button via .value ... without having
to get this value via html, is there any way I can passed the checked
value via html .. maybe with syntax like n4.checked.value or
something..

Many thanks.

<html>
<body>
WIDTH: <input type=text name=n1 value=5>
<br>
LENGTH:<input type=text id=n2 value=10>
<br>
EXTRA:<input type=text id=n3 value=20>
<br>
<br>
CHOOSE STYLE:
<br>

<input type="radio" checked name=n4 value=3>Green
<input type="radio" name=n4 value=5>Red
<input type="radio" name=n4 value=7>Blue

<br>
<br>
<input type=button value="Calulate Estimate"
onclick="mmm(n1.value,n2.value,n3.value,n4.value)" >
<br>
<br>
<br>
<br>
TOTAL COST:
<input type=text id=t value=0>
<script defer>
function mmm(x1,x2,x3,x4) {
var j1 = +x1;
var j2 = +x2;
var j3 = +x3;
var j4 = +x4;
alert(j4);
var d=document;
var el=d.getElementById("t");
el.value=Math.round(((j1*j2)+j3)*j4);
}
</script>

</body>
</html>
Jul 23 '05 #1
2 12318
> <html>
<body>
First of all: Please add a <form> tag here.
WIDTH: <input type=text name=n1 value=5>
<br>
LENGTH:<input type=text id=n2 value=10>
<br>
EXTRA:<input type=text id=n3 value=20>
<br>
<br>
CHOOSE STYLE:
<br>

<input type="radio" checked name=n4 value=3>Green
<input type="radio" name=n4 value=5>Red
<input type="radio" name=n4 value=7>Blue

<br>
<br>
<input type=button value="Calulate Estimate"
onclick="mmm(n1.value,n2.value,n3.value,n4.value)" >
You should better use document.forms[0].n1.value instead of n1.value, and so
on
<br>
<br>
<br>
<br>
TOTAL COST:
<input type=text id=t value=0>
Please add the closing </form> tag here.
<script defer>
You can't directly access the radio button value on the same web site. n4 is
an array of three values here. In order to get the current value, implement
the following function:
function radioValue(radio) {
var i;
for (i=0; i<radio.length; i++)
if (radio[i].checked)
return radio[i].value;
// otherwise no radio button of the radio button group is selected
return "undefined";
}
function mmm(x1,x2,x3,x4) {
var j1 = +x1;
var j2 = +x2;
var j3 = +x3;
var j4 = +x4;
alert(j4);
var d=document;
var el=d.getElementById("t");
You should only calculate the total cost, if one of the radio buttons was
selected. So add here:
if (radioValue(document.forms[0].n4) != "undefined")
el.value=Math.round(((j1*j2)+j3)*j4);
}
</script>

</body>
</html>
Finally change the following line onclick="mmm(n1.value,n2.value,n3.value,n4.value)" >

to that one:
onclick="mmm(document.forms[0].n1.value,document.forms[0].n2.value,document.
forms[0].n3.value,radioValue(document.forms[0].n4))">

Nice greetings from
Thomas
Jul 23 '05 #2
I figured out the below, found a function on this very forum that sets
the value of the checked radio.

function returnSelection(theRadio) {
var selection=null;
for(var i=0; i < theRadio.length; i++) {
if(theRadio[i].checked) {
selection=theRadio[i].value;
return selection;
}
} return selection;
}

Thanks.

ja***@cyberpine.com wrote in message news:<ef**************************@posting.google. com>...
The following (likely far from imperfect code), reports a value of NaN
in the j4 display. I suppose the problem is I am not really passing
the "checked" value of the radio button via .value ... without having
to get this value via html, is there any way I can passed the checked
value via html .. maybe with syntax like n4.checked.value or
something..

Many thanks.

<html>
<body>
WIDTH: <input type=text name=n1 value=5>
<br>
LENGTH:<input type=text id=n2 value=10>
<br>
EXTRA:<input type=text id=n3 value=20>
<br>
<br>
CHOOSE STYLE:
<br>

<input type="radio" checked name=n4 value=3>Green
<input type="radio" name=n4 value=5>Red
<input type="radio" name=n4 value=7>Blue

<br>
<br>
<input type=button value="Calulate Estimate"
onclick="mmm(n1.value,n2.value,n3.value,n4.value)" >
<br>
<br>
<br>
<br>
TOTAL COST:
<input type=text id=t value=0>
<script defer>
function mmm(x1,x2,x3,x4) {
var j1 = +x1;
var j2 = +x2;
var j3 = +x3;
var j4 = +x4;
alert(j4);
var d=document;
var el=d.getElementById("t");
el.value=Math.round(((j1*j2)+j3)*j4);
}
</script>

</body>
</html>

Jul 23 '05 #3

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

Similar topics

1
by: monika | last post by:
hi ... I have an asp page which has 3 buttons. <p align="center"><input class="button" type="button" onClick="location='welStudent.asp';" value="Click to write a new story"></p> <p...
2
by: Richard | last post by:
**** Post for FREE via your newsreader at post.usenet.com **** HI, I am working on a project where I need to input data to a (local) HTML page using multiple form elements, such as text,...
6
by: Ken Loomis | last post by:
Hello: On a web page I check the value of a cookie and set one of two radio buttons accordingly. function setRadioForm() { var myCookie = document.cookie; var OpenOnly =...
1
by: Leszek | last post by:
Hi. I have a form like this: <form name="dane" action="wyszukiwanie.php" method="post" onSubmit="return verify_form()"> <input maxlength="30" size="35" name="family_name" value=""> </form>...
1
by: Jerim79 | last post by:
I have a simple 3 page registration form. One form, one "data validation" script and one "insert into database" script. The customer bounces back and forth from the form to the verification script...
3
w33nie
by: w33nie | last post by:
I want to disable the text boxes, captain_name and captain_email, but only if the radio button, captain_guarantee, has NOT been checked. how do i do this? <form name="formTeamApplication"...
0
by: cyberdawg999 | last post by:
Greetings all in ASP land I have overcome one obstacle that took me 2 weeks to overcome and I did it!!!!! I am so elated!! thank you to all who invested their time and energy towards helping me...
5
by: Fran Jakers | last post by:
Hello all, I'm new to all this and I could really use some help. I've searched the web but cannot find an answer. I have an HTML form with 3 radio buttons and a search field that calls a...
3
by: ToddFur | last post by:
I see several postings on this but I am still unable to figure out my problem. I can pass the values of my text field but not radio button (or even checkboxes). My PHP file <?php //declare...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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
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
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
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...

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.