# How to test which button is checked of an existing radio buttons family?

**URL:** https://discourse.processing.org/t/how-to-test-which-button-is-checked-of-an-existing-radio-buttons-family/2849
**Category:** Coding Questions
**Created:** [August 22, 2018, 1:14pm UTC](https://discourse.processing.org/t/how-to-test-which-button-is-checked-of-an-existing-radio-buttons-family/2849 "2018-08-22T13:14:47Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![Laurent](https://avatars.discourse-cdn.com/v4/letter/l/ce7236/32.png) [@Laurent](https://discourse.processing.org/u/Laurent)
#### Post date: [August 22, 2018, 1:14pm UTC](https://discourse.processing.org/t/how-to-test-which-button-is-checked-of-an-existing-radio-buttons-family/2849/1 "2018-08-22T13:14:47Z")

</div>

Hi everyone. I’m used to write my own statitc html code into the index.html file of p5.js sketch. So I use the select() function of the DOM library to access html elements such INPUT of type text/passwordor or SELECT html elements and the use the value() function of the returned HTML object like in this sample code:

index.html

```auto
       <input id='firstname' class='inputName' type='text' placeholder='Firstname' value='John'/>
       <input id='lastname' class='inputName' type='text' placeholder='Lastname' value='Doe'/>

```

js:

```auto
  firstname = select("#firstname").value();
  lastname = select("#lastname").value();

```

It also works fine with SELECT elements too. But not with elements of radio types… For instance, the following code returns an error in the browser :

index.html

```auto
        <input type="radio" name="gender" id="femaleRadio" value="female">
        <input checked type="radio" name="gender" id="maleRadio" value="male">

```

js.code

` alert(select("gender").value());`

I get the following error message into the browser (Chrome):

> Uncaught TypeError: Cannot read property ‘value’ of null

I’ve found an alternative with pure Javascript (see below), but it’s embarrassing with students into a beginners lesson. It’s very easy to read value of various HTML input, excepted radio buttons…

The p5.js documentation shows samples that works fine with dynamically generated radio button 🙂

[https://p5js.org/reference/#/p5/createRadio](https://p5js.org/reference/#/p5/createRadio)

Any idea? Thanks a lot.

Laurent

Pure JS “heavy” alternative:

```auto
  genderRadios = document.getElementsByName("gender");
  
  if(genderRadios[0].checked){
 
    gender = "female";    
    
  } else if(genderRadios[1].checked) {
    
    gender = "male";
    
 }

```

---

<div class="post-metadata">

### Author: ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)
#### Post date: [August 22, 2018, 1:56pm UTC](https://discourse.processing.org/t/how-to-test-which-button-is-checked-of-an-existing-radio-buttons-family/2849/2 "2018-08-22T13:56:11Z")

</div>

> [@Laurent](#):
>
> `alert(select("gender").value());`

According to method p5::**select()**'s reference: [reference | p5.js](http://p5js.org/reference/#/p5/select) 👨‍🏫

> Searches the page for an element with the given ID, class, or tag name (using the ‘#’ or ‘.’ prefixes to specify an ID or class respectively, and none for a tag) and returns it as a p5.Element.

When passing a String to p5::**select()** w/ no ‘#’ or ‘.’ prefix in it, it means that String is the `<tag>`'s name itself.

Here’s some online p5js sketch which I use p5::**select()** to get the element w/ a specified `<tag>` name: 🙃

> <https://gist.github.com/GoSubRoutine/5fbc03e019c53254a2ba7e7fd3318b45>

```auto
createP('Temp: '.bold() + `<temp>${weather.main.temp}</temp><deg>${F}</deg>`);
createP('Wind: '.bold() + `<wind>${weather.wind.speed}</wind><vel>${MPH}</vel>`);

temp = select('temp'), deg = select('deg');
wind = select('wind'), vel = select('vel');

```

> [@Laurent](#):
>
> `genderRadios = document.getElementsByName("gender");`

I don’t think p5js got any equivalent for **getElementsByName()**, which searches for all elements w/ a specified _name_ attribute!

Instead p5js got p5::**selectAll()**: [reference | p5.js](https://p5js.org/reference/#/p5/selectAll)  
Which can search for all elements w/ a specified `<tag>` or a _class_ attribute name:

> Searches the page for elements with the given class or tag name (using the ‘.’ prefix to specify a class and no prefix for a tag) and returns them as p5.Elements in an array.

---

<div class="post-metadata">

### Author: ![kfrajer](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kfrajer/32/196_2.png) [@kfrajer](https://discourse.processing.org/u/kfrajer)
#### Post date: [August 22, 2018, 2:07pm UTC](https://discourse.processing.org/t/how-to-test-which-button-is-checked-of-an-existing-radio-buttons-family/2849/3 "2018-08-22T14:07:01Z")

</div>

```auto
<!DOCTYPE html>
<html>
<body>

<form action="#" method="get">
  <input type="radio" name="gender" value="male" checked> Male<br>
  <input type="radio" name="gender" value="female"> Female<br>
  <input type="radio" name="gender" value="other"> Other <br>
  <br>
  
  <input type="text" id="csel" size="50"> 
</form> 

<script>
    var options = document.forms[0];
    var txt = "";
    var i;
    for (i = 0; i < options.length; i++) {
        if (options[i].checked) {
            txt = txt + options[i].value + " ";
        }
    }    
    document.getElementById("csel").value = "Selected: " + txt;

</script>

</body>
</html>

```

Similar running example: [https://www.w3schools.com/jsref/prop\_radio\_checked.asp](https://www.w3schools.com/jsref/prop_radio_checked.asp)

Kf
