1

I have this code

 $('#Submit').click(function(event) {
            var checked = $('.inputchbox'); ......IE8
            var ids= checked.map(function() {
                return $(this).val();
            }).get().join(',');
             alert(ids);
        });

This Code return all the values which is there for Checkboxbox (.inputchbox is class for the checkbox)

but If I give somethign like this

var checked = $('.inputchbox input:checkbox:checked');
or
var checked = $('.inputchbox input[type=checkbox]:checked');
or
var checked = $('.inputchbox input[name=chk]:checked');
or 
var checked = $('.inputchbox').find('input[type=checkbox]:checked');

if i am giving like this nothing is working for me I am not getting the result in IE8?

 var checked = $('.inputchbox'); .....this is working but I am getting the checkboxes ids its doesnot checking wheather its checked or not.. 

I need to get only checked chekcbox id's

thanks

1
  • Is the .inputchbox element the same as the checkbox, or is it a parent of the checkbox as your code implies? Commented Jun 16, 2010 at 21:19

3 Answers 3

4

How about

var checked = $('.inputchbox:checked');

? The problem with your snippets is that the space you're using is the descendant selector and will look down into the DOM. The .find() method does the same. g.d.d.c was correct in replying that .filter() instead of .find() should work equally well (but a marginal amount slower):

var checked = $('.inputchbox').filter(':checked');
Sign up to request clarification or add additional context in comments.

4 Comments

His last snippet does not do the same thing AFAIK. If his checkboxes have classes, then $('.inputchbox').find() will return children of his checkbox (not likely to exist). He could also probably use $('.inputchbox').filter(':checked') to get there.
Thanks, you're absolutely right. I should RTFM (RTFApiDocumentation in this case)
Your first solution is better than the one with .filter(). Why retrieve more elements than you need, only to filter some out? (Assuming the class is on the input element.)
Maybe I should change my name - M is my initial, van belongs to my surname :) But I only included it to show there's also something very similar to the .find() solution, except for the final four letters. I also warned him that it's slower.
0

When you include a space in your selector it implies children. If you want to get by class with attributes you need to do something like this: $('.inputchbox:checked')

Comments

0

A space in a selectors indicates an ancestor-descendant relationship, i.e. .inputchbox is an ancestor of your checkbox, which from your other example, seems incorrect.

Try:

$('input.inputchbox:checkbox:checked');

Etc.

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.