163

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!

Rio
  • 14,182
  • 21
  • 67
  • 107

6 Answers6

357

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

$('#myinput').is(':checkbox')
Ken Browning
  • 28,693
  • 6
  • 56
  • 68
  • 1
    This is the method I always use. – Topher Fangio Sep 28 '09 at 18:52
  • You really can't get any simpler than that. – KyleFarris Sep 28 '09 at 20:48
  • 10
    Why use the selector engine for this? It's completely unnecessary. – Tim Down Sep 29 '09 at 12:14
  • 8
    @Tim Down I take your point that there might be more efficient ways to accomplish the same thing. I suppose I like this version because the code is very readable. – Ken Browning Sep 29 '09 at 15:31
  • 2
    @KenBrowning: Fair enough. I was fairly anti-jQuery in 2009, particularly the prevalent SO attitude of blindly using jQuery for everything; my views have mellowed a bit since and I'd undo my downvote if it were possible. However, I still think I had a point. Alternative (which I can't deny is more verbose): `var myInput = $("myinput")[0]; var isCheckbox = myInput.nodeName.toLowerCase() == "input" && myInput.type == "checkbox";` – Tim Down May 05 '16 at 10:03
  • Despite it is uneficcient comparing to any clean JavaScript solution, it's still executing faster than You can blink our eye and this is the shortest solution. I believe that until there is no difference in execution time, You can stay with shortest and unefficient method. – instead Jan 03 '18 at 05:26
23
>>> a=$("#communitymode")[0]
<input id="communitymode" type="checkbox" name="communitymode">
>>> a.type
"checkbox"

Or, more of the style of jQuery:

$("#myinput").attr('type') == 'checkbox'
Esteban Küber
  • 36,388
  • 15
  • 79
  • 97
  • Perfectly valid. Not the simplest. – KyleFarris Sep 28 '09 at 20:48
  • 6
    It saves using jQuery's selector engine, the use of which here is completely inappropriate. The first variant is much the better, since avoids any possibility of jQuery's confused `attr()` function messing anything up. – Tim Down Sep 29 '09 at 09:39
  • 3
    @Tim Down is right--you should change `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
  • 1
    In Chome, your second example is case sensitive: http://jsfiddle.net/gtza0uuL/. It is not with .prop, but I don't know if that can be gauranteed. Note: you can't just do .toLowerCase() either because 'type' may be undefined. – xr280xr Jun 29 '15 at 22:49
13
$("#myinput").attr('type') == 'checkbox'
chaos
  • 122,029
  • 33
  • 303
  • 309
6

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

document.querySelector('#myinput').getAttribute('type') === 'checkbox'
George
  • 1,552
  • 1
  • 17
  • 26
2

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'); };
Anatoliy
  • 29,485
  • 5
  • 46
  • 45
2
$('#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

Adi Prasetyo
  • 1,006
  • 1
  • 15
  • 41
Code.king
  • 37
  • 1
  • 4