[jquery] Jquery set radio button checked, using id and class selectors

Is it possible to set a radio button to checked using jquery - by a class and an id?

For example:

$('input:radio[class=test1 id=test2]).attr('checked', true);

I only seem to be able to set it by id OR class but not by both.

This question is related to jquery button checked

The answer is


"...by a class and a div."

I assume when you say "div" you mean "id"? Try this:

$('#test2.test1').prop('checked', true);

No need to muck about with your [attributename=value] style selectors because id has its own format as does class, and they're easily combined although given that id is supposed to be unique it should be enough on its own unless your meaning is "select that element only if it currently has the specified class".

Or more generally to select an input where you want to specify a multiple attribute selector:

$('input:radio[class=test1][id=test2]').prop('checked', true);

That is, list each attribute with its own square brackets.

Note that unless you have a pretty old version of jQuery you should use .prop() rather than .attr() for this purpose.


Examples related to jquery

How to make a variable accessible outside a function? Jquery assiging class to th in a table Please help me convert this script to a simple image slider Highlight Anchor Links when user manually scrolls? Getting all files in directory with ajax Bootstrap 4 multiselect dropdown Cross-Origin Read Blocking (CORB) bootstrap 4 file input doesn't show the file name Jquery AJAX: No 'Access-Control-Allow-Origin' header is present on the requested resource how to remove json object key and value.?

Examples related to button

How do I disable a Button in Flutter? Enable/disable buttons with Angular Disable Button in Angular 2 Wrapping a react-router Link in an html button CSS change button style after click Circle button css How to put a link on a button with bootstrap? What is the hamburger menu icon called and the three vertical dots icon called? React onClick function fires on render Run php function on button click

Examples related to checked

Jquery set radio button checked, using id and class selectors What is the proper way to check and uncheck a checkbox in HTML5? How to check if "Radiobutton" is checked? How to count check-boxes using jQuery? if checkbox is checked, do this Radio button checked event handling Find out whether radio button is checked with JQuery? How do I see which checkbox is checked? Setting "checked" for a checkbox with jQuery