# Javascript Help - accessing radio buttons

**URL:** <https://boards.straightdope.com/t/javascript-help-accessing-radio-buttons/547504>\
**Category:** Factual Questions\
**Created:** [July 21, 2010, 7:03pm UTC](https://boards.straightdope.com/t/javascript-help-accessing-radio-buttons/547504 "2010-07-21T19:03:45Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Kinthalis](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/kinthalis/32/16084_2.png) [@Kinthalis](https://boards.straightdope.com/u/Kinthalis)\
**Post date:** [July 21, 2010, 7:03pm UTC](https://boards.straightdope.com/t/javascript-help-accessing-radio-buttons/547504/1 "2010-07-21T19:03:45Z")

</div>

Damn if I can get this.

I have a function in javascript that accepts a string containing the radio button group name, a boolean (the intended checked property value of the radio button) and an integer that points to the button in the group.

```auto

function ChangeHTML(id, val, radio) 
{		
	
// var HTMLElement = document.getElementById(id[radio]);
// HTMLElement.checked = val;

// var HTMLElement = document.getElementByTagName(id);
// HTMLElement[radio].checked = val;

// var HTMLElement = document.getElementByTagName(id);
// HTMLElement[radio].checked = val;

}

```

I’ve tried the above three statements and none of them seem to work.

The following works fine with checkboxes btw:

```auto

                var HTMLElement = document.getElementById(id);
		HTMLElement.checked = val;

```

Anyone care to help me out?

---

<div class="post-metadata">

**Author:** ![ZipperJJ](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/zipperjj/32/211_2.png) [@ZipperJJ](https://boards.straightdope.com/u/ZipperJJ)\
**Post date:** [July 21, 2010, 7:29pm UTC](https://boards.straightdope.com/t/javascript-help-accessing-radio-buttons/547504/2 "2010-07-21T19:29:36Z")

</div>

Well see, a radio group is a group of radios, so it can’t be checked or not. A single radio can be checked, tho.

So you have to loop through each item in the group and if one of them is checked, then you can say that the group is checked.

Such as

```auto

for (i=myradiobuttonname.length-1; i > -1; i--) {
if (myradiobuttonname*.checked) {
myvalueiwanttoget = myradiobuttonname*.value;
}

```

I don’t know if that syntax is exactly right, but hopefully it put you down the right path…
