New Post has been published on Sevenkb.com
New Post has been published on http://www.sevenkb.com/jquery/how-to-check-and-uncheck-a-html-checkbox-using-jquery/
How to check and uncheck a html checkbox using JQuery
You can check or uncheck a checkbox element or a radio button using the .prop() or .attr() methods both examples given bellow.
The difference between attributes and properties can be important in specific situations. Before jQuery 1.6, the .attr() method sometimes took property values into account when retrieving some attributes, which could cause inconsistent behavior. As of jQuery 1.6, the .prop() method provides a way to explicitly retrieve property values, while .attr() retrieves attributes.
We will assume that we have checkbox or raidio button with id “myoptions”
check or uncheck a checkbox or radio button element using .attr() method , Replace Id myoptions with your checkbox or radio button id or class.
$('#myoptions').attr('checked','checked'); // checked
$('#myoptions').removeAttr('checked'); // uncheck
check or uncheck a checkbox or radio button element using .prop() method , Replace Id myoptions with your checkbox or radio button id or class. .prop() is best to use.
$( "#myoptions" ).prop( "checked", true ); // checked
$( "#myoptions" ).prop( "checked", false); // uncheck
.attr changes the attributes for that html tag.
.prop changes a property for the html tag as per the DOM tree.
As the example in this link suggests. An input field can have the attribute “value”. This will equal the default value you entered. If the user changes the value in the input field, the property “value” changes in the DOM Tree, but the original attribute is left remaining.
So basically, if you want the default value setup for an HTML tag’s attribute, use the .attr() function. If you that value can be changed by the user (such as inputs, checkbox’s, radios, etc.) use the .prop() function to get the newest value.















