JSFiddle - React, Tailwind, and code Playground
CSS
.checkBoxGroup {margin:10px; padding:10px; background:#e5e5e5}
.c {margin-left:10px; margin-top:10px;}
.rm {margin-left:10px; padding:5px; background:#eee; cursor:pointer}
.n {cursor:pointer; margin:15px;}
.l {margin-left:10px;}
JavaScript
var CheckboxGroup = function(label){
return this.init(label);
}
CheckboxGroup.prototype = {
type: "checkbox",
label: null,
description: null,
$label: null,
init: function(label){
if(!label) label = "Checkboxes";
self = this;
this.$wrap = $("<div class='checkBoxGroup'></div>");
this.$label = $("<div>"+label+"</div>").appendTo(this.$wrap);
this.$options = $("<div></div>").appendTo(this.$wrap);
this.$add = $("<div class='n'>Add Checkbox</div>").appendTo(this.$wrap);
this.$add.click(function(){
self.addOption("New Option");
});
this.options = [];
return this;
},
addOption: function(label){
$option = $("<div class='c'><input type='checkbox' name='"+label+"'><span class='l'>"+label+"</span></div>").appendTo(this.$options);
$remove = $("<span class='rm'>x</span>").appendTo($option);
$remove.click(function(){
var r=confirm("Remove Option?");
if (r==false) return;
$(this).parents('.c').remove();
});
this.options.push($option);
return $option;
},
get: function(){
return this.$wrap;
}
}
a = new CheckboxGroup("Colors");
a.addOption('red');
a.addOption('green');
a.addOption('blue');
$('body').append(a.get());
b = new CheckboxGroup("Animals");
b.addOption('dog');
b.addOption('cat');
$('body').append(b.get());