top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

How do I check/uncheck a checkbox input or radio button?

0 votes
467 views
How do I check/uncheck a checkbox input or radio button?
posted Jul 26, 2014 by Sidharth Malhotra

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

+1 vote
 
Best answer

You can check or uncheck a checkbox element or a radio button using the .prop() method:

// Check #x
$( "#x" ).prop( "checked", true );

// Uncheck #x
$( "#x" ).prop( "checked", false );

Best and shortest solution. It will work for any group of radios (with the same name).

$(document).ready(function(){
    $("input:radio:checked").data("chk",true);
    $("input:radio").click(function(){
        $("input[name='"+$(this).attr("name")+"']:radio").not(this).removeData("chk");
        $(this).data("chk",!$(this).data("chk"));
        $(this).prop("checked",$(this).data("chk"));
    });
});
answer Jul 29, 2014 by Amit Kumar Pandey
Thanks Amit Kumar.Clear Explanation.
...