Radio selector

The :radio selector selects radio buttons. See the radio tag. The equivalent of $(':radio') is $('[type=radio]'). Since :radio is not part of the CSS specification, for better performance in modern browsers it is better to use [type='radio'] instead.

Syntax

This is how we select radio buttons:

javascript$(':radio');

As with other pseudo-class selectors (those starting with ':'), it is better to precede ':' with the name of the tag or another selector, otherwise the '*' selector will be applied, i.e. $(':radio') will be treated as $('*:radio'), so it is better to use $('input:radio') instead. To select related radio buttons, you can use $('input[name=gender]:radio').

Example

Let's select the radio buttons, wrap them in a span. Then give the spans a green background and a red border using the css method:

html<form>
  <input type="button" value="button">
  <input type="file">
  <button>button</button>
  <input type="reset">
  <input type="radio" name="test">
  <input type="radio" name="test">
  <input type="text">
</form>

<

+javascript+> $('form input:radio') .wrap('

') .parent() .css({background: 'green', border: '2px red solid'}); $('form').submit(function(event) { event.preventDefault(); // prevents form submission });

<

-javascript->

See also

  • tag radio
  • method filter,
    which filters elements in a set by a given selector
  • method find,
    which searches for elements within those already found