1

I have a bunch of Input radio groups with two values (yes = 1 and no = 0)

So what I would like to have is to sum logically, means a

{input1:value} || {input2:value} || {input3:value} || {input4:value}

so either my resulting value would be 0 (false) or 1 (true)

but since I did not find such a way:

I am trying to do a rounding of the sum of all input fields So I can sum :

dummySum = {input1:value} + {input2:value} + {input3:value} + {input4:value}

In this case I will get a value from 0..4

so now I need the result like:

if (dummySum >= 1) {
   dummySum = 1;
} else {
   dummySum = 0;
}

wondering if someone here can point me to how to solve it with rsform

Preferably I would like to get it solved in javascript or even better in rsform standards

I need this behaviour during the user is working on the form. Means on every interaction on a form element (e.g. onChange() ) the calculation shall be done

  • updated my question, hope it is now much clearer? – cilap Dec 16 '18 at 15:58
  • I actually meant min([the sum], 1) not max(). What language do you need to implement this with? Php, javascript, sql? Your question needs context. Do you need this behavior when storing the data? When extracting the data? – mickmackusa Dec 16 '18 at 21:16
  • hope we talk from the same point. I would like to extend the default behaviour of rsform plugin. Means preferably this order rsform default Javascript PHP Regarding other points I have updated once more my question – cilap Dec 17 '18 at 12:52
  • Is the dummy sum meant to feed another field in the form. Or is this a matter of validation so that the other three fields cannot be selected? May we actually see the form fields that you are creating as a matter of context? RSFormsPro allows some cool javascript behaviors, but we could use a bit more clarity. – mickmackusa Dec 17 '18 at 20:52
  • There are conditionaI javascript operations on offer with RSFormsPro, but I have a sneaking suspicion that you'd be better off refactoring your form. Do you have 4 separate, yet related fields? Could they not be grouped together into a single question as a four radio button question? – mickmackusa Dec 17 '18 at 23:59
0

Name your input IDs consistently with the same prefix, for example #dummy1 #dummy2, etc. Make the default option for each input be the "0" value.

Then do:

var dummySum = 0;

jQuery('input[id^="dummy"').on("change", function(){
    dummySum = {dummy1:value} + {dummy2:value} + {dummy3:value} + {dummy4:value};
    if (dummySum >= 1) {
        dummySum = 1;
    } else {
        dummySum = 0;
    }
});
  • Since the objective is to determine of ANY of the radios are selected, it doesn't make sense to count above one. I think the logic in this question needs to be reconsidered. Perhaps a breakable loop that seeks a selected radio button and offers a quick return. Sum is unnecessary (which is why I didn't answer). – mickmackusa Feb 8 at 12:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.