I am using jQuery Uniform to style inputs/selects etcs. However, the checkbox has stopped working. I am appending
data sent from an ajax call. Once it's loaded, I use $.uniform.update("input:checkbox")
to update the new html. When attempting to (un)check the input it works only once. If I want to (un)check it again, it doesn't change at all.
I've attempted changing the Uniform Javascript so that all the actions (ie. click
, focus
, blur
etc) are under the .live
function. (ie. .live("click",
). This just stops anything from working.
Update:
I've read through the Uniform JS entirely and discovered a problem:
if(!$(elem).attr("checked")){ //box was just unchecked, uncheck span spanTag.removeClass(options.checkedClass); }else{ //box was just checked, check span. spanTag.addClass(options.checkedClass); }
The .attr("checked")
syntax fails. It will always return false. Why? I don't know. As well as this, it doesn't update the original input
to either remove the checked
attribute or to set the checked
attribute.
I've run through the official website and found it doesn't update the checkbox input. So it's not just me xP
I've Googled around a bit and found various methods for checking if a checkbox is checked however the following methods fail:
if ($(elem).attr("checked")) // The original. if ($(elem).attr("checked") == true) if ($(elem).is(":checked")) if ($(elem).checked) // and: var check = $(elem).match(/checked=/); if (check == null)
Words of guidance are very much appreciated~ :3
To check whether a Checkbox has been checked, in jQuery, you can simply select the element, get its underlying object, instead of the jQuery object ( [0] ) and use the built-in checked property: let isChecked = $('#takenBefore')[0]. checked console. log(isChecked);
A jQuery plugin to make your form controls look how you want them to. Now with HTML-5 attributes! Works well with jQuery 1.6+, but we've received patches and heard that this works with jQuery 1.3. Version 4.3.
Checking if a checkbox is checked First, select the checkbox using a DOM method such as getElementById() or querySelector() . Then, access the checked property of the checkbox element. If its checked property is true , then the checkbox is checked; otherwise, it is not.
The :checked selector works for checkboxes, radio buttons, and options of select elements. To retrieve only the selected options of select elements, use the :selected selector.
This is because UniformJs
requires that you update your modification on uniform elements if you need to change values on the form dynamically:
$('#checkbox').prop('checked',true); $.uniform.update();
If you wish to update the #checkbox
only:
$('#checkbox').prop('checked',true); $.uniform.update('#checkbox');
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With