0

I am aware that in JQuery - find a radio button by value is simply

$(":radio[value=foobar]") 

How would I use Javascript to find a radio button by its value?

Community
  • 1
  • 1

3 Answers3

1

If requirement is to not use jQuery, you can use document.querySelector() with multiple attribute selector

document.querySelector("input[type=radio][value=foobar]")
guest271314
  • 1
  • 15
  • 104
  • 177
0

You can do something like this:

document.querySelector("input[type='radio'][value='foobar']");
Titus
  • 22,031
  • 1
  • 23
  • 33
0

I assume you meant a non-jQuery way, as jQuery is Javascript :)

A naïve approach would be to filter input elements, determine if it's a radio button and determine the value:

var inputs = document.getElementsByTagName('input');
var radios = inputs.filter(function(el) {
    return el.type == 'radio';
};
var valueRadios = radios.filter(function(radio) {
    return radio.value == 'foobar';
};
jedifans
  • 2,287
  • 1
  • 13
  • 9