Idiomatic way to find out whether a checkbox is checked?
curioustechizen opened this issue · comments
I'm having trouble using Chibi to determine whether a checkbox is checked.
<input type="checkbox" id="mycb">
I tried the following:
$('#mycb').checked //==undefined
$('#mycb').val() //=="on" if checkbox is ON or "null" if it is off
$('#mycb').attr('checked') //==null
I'm not sure if I should depend on the .val()
returning on
in all browsers.
On further inspection, I noticed that $('#mycb')
returns an array instead of just a single checkbox (this is in Chrome 44 on Linux). Thus, I can use $('#mycb')[0].checked
but again this seems to contradict the Chibi docs.
So, whats the idiomatic way to do this in Chibi? I have currently by-passed Chibi and am directly doing document.getElementById('mycb').checked
for this particular case, but I'd prefer to use Chibi if there is a way.
Not sure if this works
if ($('#mycb:checked').length) {
}
@Van-Nguyen @curioustechizen In 3.0 I've changed the behaviour of Chibi to return first found, rather than array of all form element values (similar to JQuery) with each
method available for when you need to loop through all.
$('#mycb')[0].checked should work but I'm thinking to add a checked(boolean)
method. With no arguments, will return boolean for first found, with boolean argument used to check/uncheck all matching.
How does this sound?
The checked(boolean)
method sounds great to me
@curioustechizen I've added this method to 3.0.7