I have a form that lets users select checkboxes. I am able to create an array that takes the textContent of the checkbox. It works whenever they are selected, however, I would like to have the text removed from the array whenever a box is unchecked. Currently, when a box is unchecked it adds the box that is unchecked to the array. I realize that is because anytime the on change function happens (i.e. checked/unchecked) my code will populate the array. I am just looking for something that will remove the string whenever it is unchecked. Any suggestions?
html:
<section id="extra-features">
<div class="span3">
<label class="checkbox" for="Checkbox1">
<input type="checkbox" class="sum" value="50" data-toggle="checkbox"> Instagram
</label>
<label class="checkbox">
<input type="checkbox" class="sum" value="50" data-toggle="checkbox"> Review site monitoring
</label>
<label class="checkbox">
<input type="checkbox" class="sum" value="50" data-toggle="checkbox"> Google+
</label>
<label class="checkbox">
<input type="checkbox" class="sum" value="50" data-toggle="checkbox"> LinkedIn
</label>
</div>
<div class="span3">
<label class="checkbox">
<input type="checkbox" class="sum" value="50" data-toggle="checkbox"> Pinterest
</label>
<label class="checkbox">
<input type="checkbox" class="sum" value="50" data-toggle="checkbox"> FourSquare
</label>
<label class="checkbox">
<input type="checkbox" class="sum" value="50" data-toggle="checkbox"> Tumblr
</label>
<label class="checkbox">
<input type="checkbox" class="sum" value="50" data-toggle="checkbox"> Advertising
</label>
</div>
</section>
Javascript:
var options = [];
var inputs = document.getElementsByClassName('sum'),
for (var i=0; i < inputs.length; i++) {
inputs[i].onchange = function() {
if(this.checked){
options.push(this.parentNode.textContent.trim());
}
}
JSFIDDLE: http://jsfiddle.net/rynslmns/4dFGd/