To check if an element is a checkbox: Use the tagName property to check if the value is an input element. Verify that the type property on the element has a value of checkbox . If both conditions are met, the element is a checkbox.
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);
We can check the status of a radio button by using the :checked jQuery selector together with the jQuery function is . For example: $('#el').is(':checked') . It is exactly the same method we use to check when a checkbox is checked using jQuery.
You can use the pseudo-selector :checkbox
with a call to jQuery's is
function:
$('#myinput').is(':checkbox')
>>> a=$("#communitymode")[0]
<input id="communitymode" type="checkbox" name="communitymode">
>>> a.type
"checkbox"
Or, more of the style of jQuery:
$("#myinput").attr('type') == 'checkbox'
$("#myinput").attr('type') == 'checkbox'
A non-jQuery solution is much like a jQuery solution:
document.querySelector('#myinput').getAttribute('type') === 'checkbox'
Use this function:
function is_checkbox(selector) {
var $result = $(selector);
return $result[0] && $result[0].type === 'checkbox';
};
Or this jquery plugin:
$.fn.is_checkbox = function () { return this.is(':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