Reputation: 1054
I have a field from the db called sections and I have the following checkbox control on the markup.
<asp:CheckBox runat="server" ID="sections" />
<label for="sections">Section values</label>
Then on codebehind, I use this code to render checkbox control with value property and actual value of Sections displayed just the way I want it.
Me.sections.InputAttributes.Add("Value", "Miracle Alley")
My issues now are 2 fold:
1, what is the best way to add CheckAll/UncheckAll feature to the markup so users can check a checkbox or checkboxes to display a value or values associated with checked box(es)?
2, We would like to declare a variable and assign the value of checked box to this variable. For instance, if sections is checked then var sectionsList = sections values.
Is this possible?
If not, any alternative solutions?
Thanks a lot in advance.
Upvotes: 0
Views: 78
Reputation: 144689
Try the following:
1) please note that you should have 2 elements with IDs of #checkAll
and #unCheckAll
and load jQuery library :)
$(document).ready(function(){
$('#checkAll').click(function(){
$('input[type="checkbox"]').prop('checked', true)
})
$('#unCheckAll').click(function(){
$('input[type="checkbox"]').prop('checked', false)
})
})
2)
$('input[type="checkbox"]').change(function(){
var sectionsList = $('input[type="checkbox"]:checked').map(function(){
return this.value
})
console.log(sectionsList)
})
Upvotes: 1