samedi 10 mars 2018

Save state of checkbox to boolean key in object

I'm building a tool to try out different party coalitions (for educational purposes).

I figured out how to sum the assigned values of a checkbox, but now I have these keys in my objects that are unused. I'd like to have the state of the checkbox save to this key (majority = true), so I can use it later on to display a graph.

// sum up the seats assigned to the majority
$('input:checkbox').change(function() {
  var total = 0;
  $('input:checkbox:checked').each(function() { // iterate through each checked element.
    total += isNaN(parseInt($(this).val())) ? 0 : parseInt($(this).val());
  });
  document.getElementById('total').innerHTML = total;

  if (total > 75) {
    document.getElementById('success').innerHTML = 'Je hebt een meerderheid bereikt in de Kamer van Volksvertegenwoordigers!';

  } else {
    document.getElementById('success').innerHTML = 'Nog geen meerderheid bereikt';
  }

  var minority = 150 - total;
  document.getElementById('minority').innerHTML = 'De oppositie heeft ' + minority + ' zetels.';

});

How I set the seats as value for the checkbox:

displayParty() {
    var result = '<li><input type="checkbox" value="' + this._seats + '" class="majority" id="' + this.name + '"> ' + this.color + ' | ' + this.name + ' | ' + this.seats + '</li>';
    return result;
  }

Complete fiddle: https://jsfiddle.net/silvia/drq919t2/218/

Any other feedback is welcome! Just starting out and piecing together code... Thanks!




Aucun commentaire:

Enregistrer un commentaire