2

Possible Duplicate:
select all checkboxes command jquery, javascript

I am having trouble selecting all checkboxes in a div with jquery. This is the code that I currently have for the function

function selectAllCheckBoxes() {
        $('#additionalrecords input[type="checkbox"]').attr('checked', 'checked');
        }

Any help is greatly appreciated.

6
  • 1
    Do you mean selecting in the sense that the wrong elements are chosen or that their state is not changed? Either way, it seems to work. The selector by itself is correct, maybe your HTML is different but as you didn't post it we cannot say anything about it. Commented Jan 12, 2012 at 13:20
  • 1
    There is nothing wrong with this code. Debug your selector console.log($('#additionalrecords input[type="checkbox"]')). Commented Jan 12, 2012 at 13:22
  • I want to mark all checkboxes as checked. Commented Jan 12, 2012 at 13:30
  • Ok, then there is nothing wrong with the code you posted. If you don't provide more information, we cannot help you. Voting to close... Commented Jan 12, 2012 at 13:31
  • @BWoods, can you post the relevant parts of your markup, especially the additionalRecords <div> element? Commented Jan 12, 2012 at 13:32

4 Answers 4

6

On jQuery 1.6 and higher, try using prop() instead of attr():

$("#additionalrecords input:checkbox").prop("checked", true);
Sign up to request clarification or add additional context in comments.

2 Comments

You are right, but I don't think this will solve the problem.
@Felix, I think so too, but I cannot help more without seeing the questioner's markup.
3

try

$('#additionalrecords input[type="checkbox"]').attr('checked', true);

4 Comments

attr('checked', 'checked') is the right way to mark a checkbox.
yeah, you are right. I checked jquery FAQ. Both ways are right.
.attr is not the right way for anything, especially for marking a checkbox :'(
it could be not the best way, but it's a valid way
1

use $("#additionalrecords input:checkbox").attr("checked", true);

And here is the jsbin http://jsbin.com/akuzus/edit#html,live

Comments

0

Instead of setting 'checked' attr to 'checked' try true

$('#additionalrecords input:checkbox').attr('checked', true);

http://jsfiddle.net/djgraff209/mqk8L/

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.