jQuery check if an input is type checkbox?

JavascriptJqueryJquery Ui

Javascript Problem Overview


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!

Javascript Solutions


Solution 1 - Javascript

You can use the pseudo-selector :checkbox with a call to jQuery's is function:

$('#myinput').is(':checkbox')

Solution 2 - Javascript

>>> a=$("#communitymode")[0]
<input id="communitymode" type="checkbox" name="communitymode">
>>> a.type
"checkbox"

Or, more of the style of jQuery:

$("#myinput").attr('type') == 'checkbox'

Solution 3 - Javascript

$("#myinput").attr('type') == 'checkbox'

Solution 4 - Javascript

A non-jQuery solution is much like a jQuery solution:

document.querySelector('#myinput').getAttribute('type') === 'checkbox'

Solution 5 - Javascript

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'); };

Solution 6 - Javascript

$('#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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionRioView Question on Stackoverflow
Solution 1 - JavascriptKen BrowningView Answer on Stackoverflow
Solution 2 - JavascriptEsteban KüberView Answer on Stackoverflow
Solution 3 - JavascriptchaosView Answer on Stackoverflow
Solution 4 - JavascriptGeorgeView Answer on Stackoverflow
Solution 5 - JavascriptAnatoliyView Answer on Stackoverflow
Solution 6 - JavascriptCode.kingView Answer on Stackoverflow