168

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!

6 Answers 6

363

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

$('#myinput').is(':checkbox')
Sign up to request clarification or add additional context in comments.

4 Comments

Why use the selector engine for this? It's completely unnecessary.
@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.
@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";
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.
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'

4 Comments

Perfectly valid. Not the simplest.
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 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.
In Chome, your second example is case sensitive: 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.
15
$("#myinput").attr('type') == 'checkbox'

Comments

7

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

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

Comments

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

1 Comment

Remember use code refactor when you paste code in your questions and answers.
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'); };

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.