How to check a radio button with jQuery

0 votes

I was trying to check a radio button with jQuery. Here's my code:

<form> 
          <div id='type'> 
                  <input type='radio' id='radio_1' name='type' value='1' /> 
                  <input type='radio' id='radio_2' name='type' value='2' /> 
                  <input type='radio' id='radio_3' name='type' value='3' /> 
            </div> 
</form>


And the JavaScript:

jQuery("#radio_1").attr('checked', true);

Doesn't work:
jQuery("input[value='1']").attr('checked', true);

Doesn't work:
jQuery('input:radio[name="type"]').filter('[value="1"]').attr('checked', true);

Doesn't work:

Does anyone have another idea? I am unable to understand what am I missing?

Feb 23, 2022 in Java by Rahul
• 9,670 points
406 views

1 answer to this question.

0 votes

For versions of jQuery equal or above (>=) 1.6, use:

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

For versions prior to (<) 1.6, use:
 

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

One could also call click() or change() on the radio button afterwards. 

answered Feb 23, 2022 by Aditya
• 7,680 points

Related Questions In Java

+5 votes
4 answers

How to execute a python file with few arguments in java?

You can use Java Runtime.exec() to run python script, ...READ MORE

answered Mar 27, 2018 in Java by DragonLord999
• 8,450 points

edited Nov 7, 2018 by Omkar 79,572 views
0 votes
3 answers

How to check whether a file exists or not in Java?

Using nio we can check whether file ...READ MORE

answered Aug 14, 2018 in Java by Sushmita
• 6,910 points
3,584 views
0 votes
3 answers

How to check whether a string is empty or not? Is there a function for this?

str != null && str.length() != 0 alternatively str ...READ MORE

answered Sep 11, 2018 in Java by Sushmita
• 6,910 points
1,019 views
0 votes
2 answers

How to left pad a string with zero in java?

String paddedString = org.apache.commons.lang.StringUtils.leftPad("129018", 10, "0") the second ...READ MORE

answered Aug 31, 2018 in Java by Sushmita
• 6,910 points
2,863 views
0 votes
1 answer

How to check a radio button with jQuery?

For versions of jQuery equal or above ...READ MORE

answered Jun 3, 2022 in JQuery by Edureka
• 13,670 points
699 views
0 votes
1 answer

How to check a radio button with jQuery?

We can check the status of a ...READ MORE

answered Jun 10, 2022 in JQuery by rajatha
• 7,640 points
22,127 views
0 votes
0 answers

How to check radio button is checked using JQuery?

I have two radio buttons in one ...READ MORE

Jun 29, 2022 in Web Development by gaurav
• 23,260 points
294 views
0 votes
1 answer

What is jQuery?

Hey, jQuery is a fast and concise JavaScript ...READ MORE

answered Feb 14, 2020 in JQuery by kartik
• 37,510 points
1,003 views
0 votes
1 answer

jQuery fix for "Uncaught TypeError: $ is not a function" error

 Use the following lines of code in ...READ MORE

answered Feb 16, 2022 in Java by Aditya
• 7,680 points
1,689 views
0 votes
1 answer

How do I redirect with JavaScript?

To redirect to another page by Using ...READ MORE

answered Feb 16, 2022 in Java by Aditya
• 7,680 points
328 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP