0

I have following HTML:

<label><input type="checkbox" value="12" name="Cevent[]" /> Yoga</label>
<label><input type="checkbox" value="12" name="Cevent[]" /> Yoga 1</label>
<label><input type="checkbox" value="12" name="Cevent[]" /> Yoga 2</label>
<label><input type="checkbox" value="12" name="Cevent[]" /> Yoga 3</label>
<label><input type="checkbox" id="selectAllEventList" name="selectAllE"> Select All</label>

and Following Jquery:

$(document).ready(function (){

   $('#selectAllEventList').click (function () {
          $('input[name=Cevent[]]').each(function(){
          $(this).prop('checked', status);
          });

     });
 });

But i am getting Syntax error:

Error: Syntax error, unrecognized expression: [name=Cevent[]]

I think i am writing wrong Selector at input[name=Cevent[]] what is the problem how can i write it correctly because i must have to send it as a array so i have to write it as Cevent[].

3 Answers 3

2

Quote the attribute value:

$('input[name="Cevent[]"]')

Update

I assume that your code is meant to check/uncheck all the Cevent[] checkboxes when the selectAllE checkbox is changed. If that's the case, you can shorten your code significantly, because most jQuery methods apply to every element in the matched set (there's no need to use .each()):

$(document).ready(function () {
    $('#selectAllEventList').click(function () {
        $('input[name="Cevent[]"]').prop('checked', this.checked);
    });
});​
Sign up to request clarification or add additional context in comments.

3 Comments

not working :-( can you please provide the working example on jsfiddle?
@PhpSeeker - jsfiddle.net/fA3W2/1 - I've added the declaration of status, which you didn't show in your question... that may have been the problem?
@PhpSeeker - And, assuming my previous fiddle demonstrated the correct behaviour, here's a much shorter version of your code: jsfiddle.net/fA3W2/2
0

You can use \ to escape [,

Live Demo

$(document).ready(function (){

   $('#selectAllEventList').click (function () {
          $('input[name*=Cevent\\[]').each(function(){
              if( $('#selectAllEventList').is(':checked'))
                   $(this).prop('checked',true);
              else
                   $(this).prop('checked',false);
          });

     });
 });​

2 Comments

not working :-( can you please provide the working example on jsfiddle?
It is on jsfiddle now, please have a look
0

Please try this

see Demo

JS

$('#selectAllEventList').click (function () {
          $('.chkbox').each(function(){
          $(this).prop('checked', true);
          });

     });

HTML

<label><input type="checkbox" class="chkbox" value="12" name="Cevent[]" /> Yoga</label>
<label><input type="checkbox" class="chkbox" value="12" name="Cevent[]" /> Yoga 1</label>
<label><input type="checkbox" class="chkbox" value="12" name="Cevent[]" /> Yoga 2</label>
<label><input type="checkbox" class="chkbox" value="12" name="Cevent[]" /> Yoga 3</label>
<label><input type="checkbox" id="selectAllEventList" name="selectAllE"> Select All</label>

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.