0

This is how my checkboxes look like since i am using Html helpers which is inside my View:

<input data-val="true" data-val-required="The isSelected field is required." id="idstoselect" name="[2].isSelected" type="checkbox" value="true" />
<input name="[2].isSelected" type="hidden" value="false" />

I am trying to implement a Check all checkbox but i am having trouble selecting all of them due to their name format..

I have my jquery inside my Layout View and it's like this as of now:

$('#selectall').click(function () {

     //how to select all of them?
});

Normally, i can do something like

$("input[name='onenameforall']").attr("checked",this.checked))

If i won't use HtmlHelpers since i will need to setup the names manually to onenameforall. Do i need to reimplement? My checkboxes go in an array of [2].isSelected. I am thinking of getting the size of the model then make a for loop from it but my functions are located inside my layout view.

9
  • $('input[type="checkbox"]').prop('checked', $(this).is(':checked')); (or give them a class name) Commented Sep 15, 2015 at 21:52
  • Oh alright that worked. I just need to provide one class name for all of them? if so then how do i select them? just replace type with class? Commented Sep 15, 2015 at 21:54
  • try this jsfiddle.net/tsjuhzfn Commented Sep 15, 2015 at 21:55
  • If you give them a class name (say ... new { @class = "checkbox" }) then its just $('.checkbox').prop(..) Commented Sep 15, 2015 at 21:56
  • Oh alright thank you so much. Commented Sep 15, 2015 at 21:57

1 Answer 1

1

You can select all checkboxes using

$('#selectall').click(function () {
    $('input[type="checkbox"]').prop('checked', $(this).is(':checked'));
});

alternatively, give the checkboxes your generating in the loop a class name

@Html.CheckBoxFor(m => m[i].isSelected, new { @class = "mycheckbox" })

and in the script

$('#selectall').click(function () {
    $('.mycheckbox').prop('checked', $(this).is(':checked'));
});
Sign up to request clarification or add additional context in comments.

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.