PDA

View Full Version : How do i find which check-box/radio-button is checked?


D2K2
09-02-2002, 03:06 PM
I am designing a form thingie to take details from a paper-version of the form. I have managed to get the details out of all the form elements with JS appart from Check-boxes and Radio buttons. Can anyone tell me how to get the information out?
(they are all named the same, just different values.)

<form name="form1">
Name: <input type="text" name="name"><br>
Age: <input type="text" name="age"><br>
Hobbies<br>
<input type="checkbox" name="hobbies" value="sports">Sports<br>
<input type="checkbox" name="hobbies" value="computing">Computing<br>
<input type="checkbox" name="hobbies" value="instruments">Instruments<br>
<input type="checkbox" name="hobbies" value="music">Music<br>
</form>

EG:
var name=document.form1.name.VALUE
var age=document.form1.age.VALUE
var hobbies=document.form1.hobbies.???

Thanks
D2K2

dagaffer
09-02-2002, 08:22 PM
I know as little as most beginners do so this is just a guess here. This is probably needlessly long but i dunno, it might not even work...

var i=0;
var hobbies;
function hob() {
for (i=0; i<=3; i++) {
if (document.form1.elements[i].checked == true) {
hobbies = document.form1.elements[i].value;
}
}
}

thats just based on if u had the 4 checkboxes in the form, ud have 2 change it accordingly 2 ur form...

ConfusedOfLife
09-02-2002, 09:41 PM
Dagaffer is right, it's the only way that I know for checking all the checkboxes, also it's no need to put ONE name for all the checkboxes, it's something that you should do for radiobuttons!;)

Also, in your if, it's no need to check them for true, just write

if ( document.form1.elements[i].checked )

and if it's true, then it works, just for simplicity!
If you need to do an action when a checkbox is checked, then instead of checking all of them, you can define an onclick event for those that you want, defining a function with an argument sending to that, indicating which check box was checked.

<input type="checkbox" value="something" onclick="doit(this.value)">

<script>
function doit(oAr)
{
switch ( oAr )
{
case "something" :
alert('hello');
break;
// write your cases in here
}
}
</script>

boywonder
09-02-2002, 09:54 PM
Yes you would need to loop through the array, however I would make some changes.
I would pass the object to the function for three reasons, you can re-use the function for other element arrays, also you can use its length to determine how many loop iterations you need. Lastly you can add or remove as many checkboxes as you want and the function does not need to be changed.
I chose to store the results in an array but you could store them in a string if you like.

<script language="JavaScript" type="text/javascript">
<!--
function getValues(el) {
var hobbies=new Array(), i=0, j=el.length;
for (i=0; i<j; i++) if(el[i].checked) hobbies[hobbies.length] = el[i].value;
alert(hobbies);
}
//-->
</script>
<a href="#" onclick="getValues(document.form1.hobbies);">get values</a>

I added an alert to illustrate
hope that helps you

EDIT: Whoops I didn't see confused's post when I submitted, I am referring to dagaffer's loop.:thumbsup: