Home  >  Article  >  Web Front-end  >  How do I programmatically check a radio button in jQuery?

How do I programmatically check a radio button in jQuery?

Barbara Streisand
Barbara StreisandOriginal
2024-11-07 16:13:03958browse

How do I programmatically check a radio button in jQuery?

Checking Radio Buttons with jQuery

In jQuery, checking a radio button can be a bit tricky. Here are a few common approaches:

Using .attr()

For jQuery versions prior to 1.6, the following syntax will work:

$("#radio_1").attr('checked', 'checked');

Using .prop()

For jQuery versions 1.6 and above, use the updated syntax:

$("#radio_1").prop("checked", true);

Additional Tips:

  • It's recommended to invoke the .change() or .click() methods on the radio button after checking it to ensure that event handlers are triggered.

Troubleshooting:

If the provided methods aren't working, double-check the following:

  • The selector matches the correct radio button.
  • The jQuery version you're using requires the appropriate syntax (attr() or prop()).
  • The radio button is not disabled.

The above is the detailed content of How do I programmatically check a radio button in jQuery?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn