Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 10-30-2007, 11:50 AM   PM User | #1
bhakti_thakkar
Regular Coder

 
Join Date: Sep 2006
Location: India Mumbai
Posts: 248
Thanks: 13
Thanked 1 Time in 1 Post
bhakti_thakkar is an unknown quantity at this point
Validating radio[] array problem

hi all,
i have radio buttons for every group on my PHP page which populate dynamically from the DB:

Yes<INPUT TYPE="radio" NAME="rollback_<?=$arr['Audit_ID']?>" id="RollBackRadio[]" value="1"><br>
No<INPUT TYPE="radio" NAME="rollback_<?=$arr['Audit_ID']?>" id="RollBackRadio[]" value="0">

<script language="javascript">
function checkRadio(){
var radios = document.mainform.elements["RollBackRadio[]"];
var radflag=false;
for (var i=0; i<radios.length; i++){
if (radios[i].checked){
radflag=true;
} else {
radflag=false;
}
}
return radflag;
}

</script>
for eg ... if there are three audits on the page then there would be three sets of radio buttons. how do i validate that atleast one radio is checked for every Audit
Note : i cannot change the name of the radio button. probably would have to play with IDs. i tried giving id to the radio but the script is getting length as 6 [for the above case where audits are 3] instead of 3 ..
how can i do it??
Thanks in advance.
bhakti_thakkar is offline   Reply With Quote
Old 10-31-2007, 10:03 AM   PM User | #2
shyam
Senior Coder

 
shyam's Avatar
 
Join Date: Jul 2005
Posts: 1,563
Thanks: 2
Thanked 163 Times in 160 Posts
shyam will become famous soon enough
if u can't change the name then change the id

Code:
Yes<INPUT TYPE="radio" NAME="rollback_1" id="RollBackRadio0_1" value="1"><br>
No<INPUT TYPE="radio" NAME="rollback_1" id="RollBackRadio0_0" value="0">
and check if anything is selected by using the group number of the radio in this case 0

Code:
function checkRadio(group){
	var cnt = 0;
	var el = document.getElementById('RollBackRadio' + group + '_' + cnt);
	var flag = false;
	while ( !!el && !flag ) {
		flag = el.checked;
		cnt++;
		el = document.getElementById('RollBackRadio' + group + '_' + cnt);
	} // endwhile
	alert( (flag ? 'something' : 'nothing') + ' is selected');
	return flag;
}
__________________
You never have to change anything you got up in the middle of the night to write. -- Saul Bellow
shyam is offline   Reply With Quote
Users who have thanked shyam for this post:
bhakti_thakkar (11-03-2007)
Old 11-03-2007, 05:49 AM   PM User | #3
bhakti_thakkar
Regular Coder

 
Join Date: Sep 2006
Location: India Mumbai
Posts: 248
Thanks: 13
Thanked 1 Time in 1 Post
bhakti_thakkar is an unknown quantity at this point
Hi Shyam,

Thanks for the helpful reply

Quote:
Originally Posted by shyam View Post
if u can't change the name then change the id

Code:
Yes<INPUT TYPE="radio" NAME="rollback_1" id="RollBackRadio0_1" value="1"><br>
No<INPUT TYPE="radio" NAME="rollback_1" id="RollBackRadio0_0" value="0">
and check if anything is selected by using the group number of the radio in this case 0

Code:
function checkRadio(group){
	var cnt = 0;
	var el = document.getElementById('RollBackRadio' + group + '_' + cnt);
	var flag = false;
	while ( !!el && !flag ) {
		flag = el.checked;
		cnt++;
		el = document.getElementById('RollBackRadio' + group + '_' + cnt);
	} // endwhile
	alert( (flag ? 'something' : 'nothing') + ' is selected');
	return flag;
}
bhakti_thakkar is offline   Reply With Quote
Old 11-03-2007, 12:31 PM   PM User | #4
BarrMan
Senior Coder

 
BarrMan's Avatar
 
Join Date: Feb 2005
Location: Israel.
Posts: 1,644
Thanks: 69
Thanked 83 Times in 82 Posts
BarrMan is on a distinguished road
Quote:
Originally Posted by shyam View Post
if u can't change the name then change the id

Code:
Yes<INPUT TYPE="radio" NAME="rollback_1" id="RollBackRadio0_1" value="1"><br>
No<INPUT TYPE="radio" NAME="rollback_1" id="RollBackRadio0_0" value="0">
and check if anything is selected by using the group number of the radio in this case 0

Code:
function checkRadio(group){
	var cnt = 0;
	var el = document.getElementById('RollBackRadio' + group + '_' + cnt);
	var flag = false;
	while ( !!el && !flag ) {
		flag = el.checked;
		cnt++;
		el = document.getElementById('RollBackRadio' + group + '_' + cnt);
	} // endwhile
	alert( (flag ? 'something' : 'nothing') + ' is selected');
	return flag;
}
Hey, Shyam. What does the !!el mean?
BarrMan is offline   Reply With Quote
Old 11-03-2007, 03:55 PM   PM User | #5
shyam
Senior Coder

 
shyam's Avatar
 
Join Date: Jul 2005
Posts: 1,563
Thanks: 2
Thanked 163 Times in 160 Posts
shyam will become famous soon enough
well its just a fancy way of checking if a particular object/property is present...the idea is that a double negative cancel each other out so the output is always a boolean !!'', !!null, !!undefined and !!0 all return false...pretty much everything else returns true
__________________
You never have to change anything you got up in the middle of the night to write. -- Saul Bellow
shyam is offline   Reply With Quote
Users who have thanked shyam for this post:
BarrMan (11-03-2007)
Old 11-03-2007, 04:03 PM   PM User | #6
BarrMan
Senior Coder

 
BarrMan's Avatar
 
Join Date: Feb 2005
Location: Israel.
Posts: 1,644
Thanks: 69
Thanked 83 Times in 82 Posts
BarrMan is on a distinguished road
I see. That's a nice trick.

Thanks
BarrMan is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 08:22 AM.


Advertisement
Log in to turn off these ads.