Check if DOM Element is a checkbox
Checks anything
function isCheckbox (element) {
return element instanceof HTMLInputElement
&& element.getAttribute('type') == 'checkbox'
}
If you're using jQuery, you can use the :checkbox
pseudo-class selector along with is
method:
if($("#that-particular-input").is(":checkbox")) {
}
Using only vanilla javascript you could do
if (el.type && el.type === 'checkbox') {
...
}
or even shorter
if ((el || {}).type === 'checkbox') {
...
}
or in modern browsers you could use matches()
if (el.matches('[type="checkbox"]') {
...
}