PDA

View Full Version : Resolved How to check dynamically created radio buttons



foneguy2
03-27-2009, 03:59 PM
I have several sets of dynamically created radio buttons. Each with a yes,no, and n/a.



<input type="Radio" name="r_1_237" id="r1_1_237" value="yes">Yes<br />
<input type="Radio" name="r_1_237" id="r2_1_237" value="no">No<br />
<input type="Radio" name="r_1_237" id="r3_1_237" value="na">N/A

<input type="Radio" name="r_1_238" id="r1_1_238" value="yes">Yes<br />
<input type="Radio" name="r_1_238" id="r2_1_238" value="no">No<br />
<input type="Radio" name="r_1_238" id="r3_1_238" value="na">N/A

<input type="Radio" name="r_2_239" id="r1_2_239" value="yes">Yes<br />
<input type="Radio" name="r_2_239" id="r2_2_239" value="no">No<br />
<input type="Radio" name="r_2_239" id="r3_2_239" value="na">N/A


The id of the NA radio buttons always start with 'r3_' and the value is always = 'na'.

I need a 'Check All N/A' button that would select all the N/A radio buttons in all the sets of buttons. I am not sure how to reference the buttons if I don't know the exact id.

Thanks

Nile
03-27-2009, 04:15 PM
Try this:


<script type="text/javascript">
var checkAll = function(begins){
for(i=0; (a=document.getElementsByTagName('input')); i++){
[i]if(a.type == "radio"){
var id = a.id.substring(0, begins.length);
if(id == begins){
a.checked = true;
}
}
}
}
</script>
<a href="javascript:void(0)" onclick="checkAll('r3_');">Check all N/a</a><br />
<input type="Radio" name="r_1_237" id="r1_1_237" value="yes">Yes<br />
<input type="Radio" name="r_1_237" id="r2_1_237" value="no">No<br />
<input type="Radio" name="r_1_237" id="r3_1_237" value="na">N/A

<input type="Radio" name="r_1_238" id="r1_1_238" value="yes">Yes<br />
<input type="Radio" name="r_1_238" id="r2_1_238" value="no">No<br />
<input type="Radio" name="r_1_238" id="r3_1_238" value="na">N/A

<input type="Radio" name="r_2_239" id="r1_2_239" value="yes">Yes<br />
<input type="Radio" name="r_2_239" id="r2_2_239" value="no">No<br />
<input type="Radio" name="r_2_239" id="r3_2_239" value="na">N/A


You can also do this for the other checkboxes, I made this so when you call the function, you type in what the id begins with, and then it finds all of those and checks them.

foneguy2
03-27-2009, 05:06 PM
Perfect! Worked like a charm.
THANKS for the quick response too.

Nile
03-27-2009, 06:13 PM
Glad to help you! Your welcome!

It seems your topic is solved... Please set the status to resolved.. To do this:
Go to your first post ->
Edit your first post ->
Click "Go Advanced" ->
Then in the drop down next to the title, select "RESOLVED"