Skip to content

Instantly share code, notes, and snippets.

@howardpanton
Created April 9, 2015 13:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save howardpanton/780101107da9de42a51c to your computer and use it in GitHub Desktop.
Save howardpanton/780101107da9de42a51c to your computer and use it in GitHub Desktop.
Bonus Question
//
// Matslab Question 5 Bonus
// Howard PAnton
//
(function() {
// Add new input option
$(".add_button").click(function(){
var value = $(".option_name").val();
$( ".checklist" ).append( "<label><input type=\"checkbox\" value=\"" + value + "\" /><span class=\"option_name\">" + value + "</span></label>" );
return false;
get_selected();
});
// <label><input type="checkbox" checked="checked" /><span class="option_name">Option 2</span></label>
var get_selected = function() {
var selected = [];
var index;
var i;
// Remove old list
$( ".items" ).remove();
// Create an Array of cheked items
$(".checklist").find("input:checked").each(function (i, ob) {
selected.push($(ob).val());
});
// Output all checked items in div
$( ".selected_options" ).append( "<ul class=\"items\">" );
for (index = 0; index < selected.length; ++index) {
$( ".selected_options ul.items" ).append( "<li><span>" + selected[index] + "</span></li>" );
++i;
}
};
// We need to remove items when checked and unchecked
$( ".checklist" ).on( "click", "input", function() {
var checked = $(this);
if ( checked.is(':checked') ) {
$(checked).attr('checked');
get_selected();
} else {
$(checked).removeAttr('checked');
get_selected();
}
});
// Create the list on doc load
$(function() {
get_selected();
});
}());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment