PDA

View Full Version : please help with radio button values


setfit
06-21-2003, 04:19 PM
Am trying to use radio buttons (yes/no) in a javascript.
i need the answers to a series of questions to have a numeric value. for example: 'yes' is worth 2 points, 'no' is worth 0 points.
after answering all the questions, a score (point total) is calculated.
i'm able to make this work with drop down boxes, but radio buttons are a mystery.
please consider me a novice with any reply.
thanks for your help.

shlagish
06-21-2003, 10:00 PM
Something like:
if(my_radio.checked=true){
points=2
}
else{
points=1
}


should work

radio buttons (http://www.w3schools.com/html/html_forms.asp) (scroll down a little)

Set and return values from radio buttons (http://www.w3schools.com/dhtml/tryit.asp?filename=try_dom_input_value) (example with editting possibility)

chrismiceli
06-22-2003, 12:13 AM
here is something simply that might help you out some

<form name="radies">
yes <input type="radio" value="1">
no <input type="radio" value="0">

yes <input type="radio" value="1">
no <input type="radio" value="0">
</form>

<script type="javascript">
fm = docuemnt.radies;
function checkR() {
for(x=0;x<fm.length - 1;x++) {
if(fm.elements[x].checked && fm.elements[x].value=="1")
total+=2;
}
}
</script>

shlagish
06-23-2003, 01:23 AM
<form name="radies">
yes <input type="radio" value="1">
no <input type="radio" value="0">

yes <input type="radio" value="1">
no <input type="radio" value="0">
</form>

<script type="javascript">
fm = docuemnt.radies;
function checkR() {
for(x=0;x<fm.length - 1;x++) {
if(fm.elements[x].checked && fm.elements[x].value=="1")
total+=2;
}
}
</script>

docuemnt should be replaced by document

setfit
06-29-2003, 02:32 PM
Thanks for the replies.
Sorry to bother you again.

i've been playing with this for a week and have not been able to make it work for what i need. (basically i'm too new at this to understand what's happening).

each question will have it's own 'yes' or 'no.' i applied group names to each set of boxes to separate them from each other.

what i still don't get is....

how do i make the total value appear in a text box?

for example 7 'yes' answers yield 14 points (2 ea.)
while 3 'no' answers yield 3 points (1 ea.)
total '17.'

thanks again

shlagish
07-03-2003, 01:41 AM
Off the top of my head:

<form name="radies">
yes <input type="radio" value="1">
no <input type="radio" value="0">

yes <input type="radio" value="1">
no <input type="radio" value="0">

points <input type="text" value="0">
</form>

<script type="javascript">
fm = document.radies;
function checkR() {
for(x=0;x<fm.length - 1;x++) {
if(fm.elements[x].checked && fm.elements[x].value=="1")
total+=2;
fm.points==total
}
}
</script>


I think this should work...

setfit
07-04-2003, 04:02 PM
i really appreciate your efforts and patience. but i still can't get this to happen (too dense).

i named the groups so i could select a radio button.
had to give them two names so the yes/no pairs would respond separately. but i still can't get it to total the values. here's what i'm trying to do...


<p>Question1 <input type="radio" value="1" name="Q1">yes <input type="radio" value="0" name="Q1">no</p>
<p>Question2 <input type="radio" value="1" name="Q2">yes <input type="radio" value="0" name="Q2">no</p>
<p>Question3 <input type="radio" value="1" name="Q3">yes <input type="radio" value="0" name="Q3">no</p>
<p><input type="text" name="score" size="5"></p>
<p><input type="button" value="tally" name="B1"></p>

when the 'tally' button is clicked, the values of Questions 1 - 3 should be added together and appear in the 'score' text box.

Skyzyx
07-04-2003, 04:09 PM
*Lightbulb clicks*

You can only have one set of radio buttons per form (i.e. one question, multiple answers).

For multiple questions with yes/no answers, you need to use checkboxes. Either one checkbox per question (on=yes, off=no) or have two boxes if there are 3-4 answers (one=yes, other=no, none=something else, both=something else else).

Radio buttons and Checkboxes were designed for different things. For yes/no question, checkboxes are the way to go.

gcowsar
03-27-2007, 04:10 PM
Here's an example of multiple radio groups in a single form.

You can try this by going to w3schools and paste the html into the panel on the left, then click 'Edit the text and click me'.

http://www.w3schools.com/js/tryit.asp?filename=tryjs_text

<html>
<body>

<script type="text/javascript">

function showResult(itemNdx)
{
var form = document.forms[0];
var result = document.getElementById('result');
var elem = form.elements[itemNdx];
var itemName = elem.name;
result.innerHTML = 'Item name: ' + elem.name + ', id: ' + elem.id;
}

</script>

<form name="input" action="">

<p>Are you a girl or a guy?</p>
<input type="radio" name="sex" id="male" onclick=showResult(0) />
<label for="male">Male</label>
<br />
<input type="radio" name="sex" id="female" onclick=showResult(1) />
<label for="female">Female</label>

<p>Are you a bird or a mammal?</p>
<input type="radio" name="kind" id="bird" onclick=showResult(2) />
<label for="bird">Bird</label>
<br />
<input type="radio" name="kind" id="mammal" onclick=showResult(3) />
<label for="mammal">Mammal</label>

</form>

<p>Result:</p>
<div id="result"></div>

</body>
</html>