working with checkboxes and radio buttons

Here is a good way to get the info from checkboxes. make sure to give them all the same name, just different values. Then wrap the checkboxes in a div container with an id.In this case #c_b is a div wrapped around the checkboxes and #t is the id of a text box im using to display the results.

function updateTextArea() {
var allVals = [];
$(‘#c_b :checked’).each(function() {
allVals.push($(this).val());
});
$(‘#t’).val(allVals);
}
$(function() {
$(‘#c_b input’).click(updateTextArea);
updateTextArea();
});

 

After you have these values, of what boxes are checked, we can save the state of the checked boxes using cookies, so that the user will not have to check the boxes again when they use them…

[code]


$(‘#t’).val(allVals);
setCookie(“responsible_parties_choice”,allVals,”30″);

[/code]

To read the cookie and set the checkboxes use the code…
[code]
// look for checked box choices in cookie, then re check them
var thecheckedchoices = getCookie(“responsible_parties_choice”).split(‘,’);

for (choice in thecheckedchoices) {
//console.log(“im in there with the cookie data of ..” + thecheckedchoices[choice]);
$(“input:checkbox[value=” + thecheckedchoices[choice] + “]”).prop(‘checked’, true);
}
[/code]

 

Radio Buttons can be extremely tricky. First thing to remember is to group them using a “name” attribute.

Then they can be selected and manipulated as a group.

$("input[type='radio'][name='commencementattendanceoption']").on("change", function(){
alert($( this ).val())
});

Now I need to validate that one of them has been chosen.This will require some serious stuff. I need an include to validate things, to work with my content display system.

 

A good way to get  a comma-separated list of checkbox IDs:

$( ":checkbox" )
.map(function() {
return this.id;
})
.get()
.join();
The result of this call is the string, "two,four,six,eight".

Leave a Reply