I'd like to find out if an input is a checkbox or not, and the following doesn't work:
$("#myinput").attr('checked') === undefined
Thank you once again!
Join Stack Overflow to learn, share knowledge, and build your career.
I'd like to find out if an input is a checkbox or not, and the following doesn't work:
$("#myinput").attr('checked') === undefined
Thank you once again!
You can use the pseudo-selector :checkbox
with a call to jQuery's is
function:
$('#myinput').is(':checkbox')
var myInput = $("myinput")[0]; var isCheckbox = myInput.nodeName.toLowerCase() == "input" && myInput.type == "checkbox";
– Tim Down
May 5 '16 at 10:03
>>> a=$("#communitymode")[0]
<input id="communitymode" type="checkbox" name="communitymode">
>>> a.type
"checkbox"
Or, more of the style of jQuery:
$("#myinput").attr('type') == 'checkbox'
attr()
function messing anything up.
– Tim Down
Sep 29 '09 at 9:39
attr()
to prop()
, afaik. attr()
doesn't always get the "real" attribute value (i.e. checked or not) from the browser. Honestly not sure why this is the case, but I learned this a while back.
– thekingoftruth
Dec 10 '12 at 20:10
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'); };
$('#myinput').is(':checkbox')
this is the only work, to solve the issue to detect if checkbox checked or not. It returns true or false, I search it for hours and try everything, now its work to be clear I use EDG as browser and W2UI