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 in Java by Rahul
• 8,980 points
57 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 by Aditya
• 7,280 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 59,281 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,900 points
2,171 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,900 points
439 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,900 points
2,113 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
287 views
0 votes
1 answer

Uncaught TypeError: Cannot read property 'msie' of undefined - jQuery tools

Hello, Use the following script tag in your ...READ MORE

answered Apr 28, 2020 in JQuery by Niroj
• 82,680 points
7,976 views
0 votes
1 answer

Uncaught Error: Bootstrap's JavaScript requires jQuery

Hello @kartik, You have provided wrong order for ...READ MORE

answered Apr 28, 2020 in JQuery by Niroj
• 82,680 points
14,621 views
0 votes
1 answer

How to make Bootstrap popover Appear/Disappear on hover instead of click?

Hello @kartik, Set the trigger option of the popover to hover instead ...READ MORE

answered May 12, 2020 in JQuery by Niroj
• 82,680 points
1,109 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 in Java by Aditya
• 7,280 points
71 views
0 votes
1 answer

How do I redirect with JavaScript?

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

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