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);
else
$('td input[type="checkbox"]').prop('checked', false);
})
});
http://jsfiddle.net/aJhm9/2/ 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().
$(".form-clear").click(function(){
$(':input').not(':button, :submit, :reset, :hidden').val('');
$(':checkbox, :radio').prop('checked', false);
});
$(".form-set").click(function(){
$(':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() {
alert($('input[name="myname"][value="b"]').length);
$('input[name="myname"][value="b"]').attr('checked','checked');
$('#b').attr('checked',true);
alert($('input[name="myname"]:checked').val());
});
http://jsfiddle.net/uL545/1/ 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
How to Bind Bootstrap Popover on Dynamic Elements
Can Jquery Read/Write Cookies to a Browser
Clear JavaScript Console in Google Chrome
How to Calculate How Many Seconds Between Two Dates
Fix the Upstream Dependency Conflict Installing Npm Packages
Understanding JavaScript Promise Object
Does Use of Anonymous Functions Affect Performance
How to Pass JavaScript Variables as Parameters to Jsf Action Method
JavaScript Strings Outside of the Bmp
Uncaught Typeerror: Cannot Use 'In' Operator to Search for 'Length' In
Adding a Slide Effect to Bootstrap Dropdown
Load Local JSON File into Variable
Angular 2 Karma Test 'Component-Name' Is Not a Known Element
How to Show Current Time in JavaScript in the Format Hh:Mm:Ss