views:

219

answers:

4

I am currently using a catch all method for all inputs on my form when it is being handled by jQuery.

$.each($(':input'),function()
{
    //stuff     
});

What I need to be able to do is see if any of those inputs is a checkbox, at the moment the only thing I can find is to say if the field is checked or not.

Any help would be greatly appreciated.

A: 

try this in .each function

if($('#myId').attr('type') == 'checkbox') alert ('checkbox');

UPDATE

$.fn.tagName = function() {            
    return this.attr("type");          
}                                      
  $(document).ready(function() {       
    alert($('#testElement').tagName());
    });
Wbdvlpr
checkbox is not a tagName, it's a type.
bobince
+3  A: 

You can do:

$.each($(":input[type=checkbox]"), function() {
    // stuff
}
Prody
What's the point of doing `:input` here instead of just `input`?
Josh Stodola
:input is wrong. since it matches all `input`, `textarea`, `select` and `button` elements. and `select` are not needed at all in this case
Tzury Bar Yochay
+4  A: 

If you want to know whether it's a checkbox inside that function:

$(':input').each(function() {
    if (this.type==='checkbox')
        ....
});

(Yeah, you can also say $(this).attr('type')==='checkbox' if you're one of those people who're dead set on using jQuery syntax for everything. But really, what's the point? It's only going to be slower and less readable.)

If you want to find only checkboxes, there's a special filter for that:

$(':checkbox').each(function() {
    ...
});
bobince
+3  A: 
$('input[type=checkbox]').each(function(){
   // stuff
});

Or even better

$('input:checkbox').each(function(){
   // stuff
})

see at http://docs.jquery.com/Selectors/checkbox

Tzury Bar Yochay