Viewing 5 replies - 1 through 5 (of 5 total)
  • Plugin Author bgermann

    (@bgermann)

    There is a way, but not a built-in way. You would have to write some JavaScript that listens on the items’ onchange events. On each event you would sum up the fields’ values and replace the Total value.

    Thread Starter ztaylor39

    (@ztaylor39)

    Excellent!!

    I’m fairly new to JS though… How would I go about writing a script to sum the values using an onchange event?

    Plugin Author bgermann

    (@bgermann)

    With jQuery, which is included in WordPress, so you do not have to load it additionally, the core lines would look like

    var offsets = [11,12,15,16,17];
    
    var changehandler = function () {
      var sum = 0;
      for (var i = 0; i < offsets.length; i++) {
        sum += jQuery('#cf_field_' + offsets[i]).val();
      }
      jQuery('#cf_field_19').val(sum);
    };
    
    for (var i = 0; i < offsets.length; i++) {
      jQuery('#cf_field_' + offsets[i]).change(changehandler);
    }
    Thread Starter ztaylor39

    (@ztaylor39)

    Awesome!

    Where exactly do I put that code?

    Plugin Author bgermann

    (@bgermann)

    An ugly solution is just to paste it in one of the field’s titles, surrounded by <script>…</script> tags. There are better solutions, like loading it in a *.js file on exactly that page, but you would have to figure it out by yourself or search the web.

Viewing 5 replies - 1 through 5 (of 5 total)

The topic ‘Automatic Total Option?’ is closed to new replies.