Jquery Attr('Checked','Checked') Works Only Once

jquery attr('checked','checked') works only once

Use prop('checked', true / false) instead of removeAttr

$('input[name=foo]').prop('checked', true);
$('input[name=foo]').prop('checked', false);

.attr('checked', 'checked') / .attr('checked', 'true') not adding attribute in html

To update boolean attribute as checked in jQuery version <1.6, you should use javascript setAttribute() method, e.g:

$("#E_DIV_H input[value='E,F']")[0].setAttribute('checked', true);

If you have more than one element to target, then you have to iterate through collection, e.g:

var els = document.querySelectorAll("#E_DIV_H input[value='E,F']");
for (var i=0; i < els.length; i++) {
els[i].setAttribute("checked", true);

Check and uncheck inputs work only one time with Jquery

Use properties instead of attributes

$(function () {
$('th input[type="checkbox"]').click(function(){
if ( $(this).is(':checked') )
$('td input[type="checkbox"]').prop('checked', true);
$('td input[type="checkbox"]').prop('checked', false);


jQuery (checked, true) works, then doesn't work?

The most important concept to remember about the checked attribute is that it does not correspond to the checked property. The attribute actually corresponds to the defaultChecked property and should be used only to set the initial value of the checkbox.

Use .prop() instead of the .attr().

$(':input').not(':button, :submit, :reset, :hidden').val('');
$(':checkbox, :radio').prop('checked', false);
$(':input').not(':button, :submit, :reset, :hidden').val('New preset value');
$(':checkbox, :radio').prop('checked', true);

.attr('checked','checked') does not work

With jQuery, never use inline onclick javascript. Keep it unobtrusive. Do this instead, and remove the onclick completely.

Also, note the use of the :checked pseudo selector in the last line. The reason for this is because once the page is loaded, the html and the actual state of the form element can be different. Open a web inspector and you can click on the other radio button and the HTML will still show the first one is checked. The :checked selector instead filters elements that are actually checked, regardless of what the html started as.

$('button').click(function() {


JQuery is changing checkbox value but the checkbox is not displaying as checked

With .attr() you set the value of an attribute in the DOM tree. Depending on the browser, browser version and jQuery version (especially 1.6), this does not always have the desired effect.

With .prop(), you manipulate the property (in this case, 'checked state'). I strongly recommend using .prop() in your case, so the following will toggle correctly:

$(this).find(".cbx input").prop('checked', true);

For further information, please refer to the documentation of .prop().

Related Topics

Leave a reply