Home › Forums › Quform WordPress › javascript if else statement help
- This topic has 10 replies, 2 voices, and was last updated 5 years, 2 months ago by Ally.
- AuthorPosts
- January 14, 2019 at 10:05 am #28213aaron1988Participant
Hi Ally,
I have a field called: Writing and another called: Estimate Overall, i need to do something like:
If Writing is 6.5 or Less then do Writing = overall score – 0.5
and if writing is greater than 6.5 then do: writing = overall score – 0.75
Writing is a checkbox field. and estimate overall is just a text based.
Hope this makes sense 🙂
Thanks Aaron
January 15, 2019 at 10:34 am #28227AllySupport StaffYou don't have permission to view this content. Please log in or register and then verify your purchases to gain access.
January 15, 2019 at 10:41 am #28229aaron1988ParticipantThanks Ally,
I think i understand :).
This is my full JS code so you can see what we are doing 🙂
// Start Yes jQuery(function ($) { var calculate = function () { var total = 0; var listening = $('.quform-field-1_18:checked').val(); if (listening && $.isNumeric(listening)) { total += parseFloat(listening); } var reading = $('.quform-field-1_19:checked').val(); if (reading && $.isNumeric(reading)) { total += parseFloat(reading); } var writing = $('.quform-field-1_20:checked').val(); if (writing && $.isNumeric(writing)) { total += parseFloat(writing); } var speaking = $('.quform-field-1_21:checked').val(); if (speaking && $.isNumeric(speaking)) { total += parseFloat(speaking); } total = Math.round(total * 2) / 2; // Display the result to the user $('#form-total').text('Total: ' + total); // Total field $('.quform-field-1_69').val(total); var overall = $('.quform-field-1_11:checked').val(); var target = $('.quform-field-1_23:checked').val(); var gap = target - overall; if(gap < 0){ gap = 0; } //GAP $('.quform-field-1_70').val(gap); }; // Calculate on page load calculate(); // Recalculate when these text input fields are changed $('.quform-field-1_11, .quform-field-1_18, .quform-field-1_19, .quform-field-1_20, .quform-field-1_21, .quform-field-1_23').on('click', calculate); }); jQuery(function ($) { $('.quform-input-1_5 input').blur(function () { $('#name').text($('.quform-field-1_5').val() + ' ' + $('.quform-field-1_10').val()); }); $('.quform-field-1_22').blur(function () { $('#taken').text($(this).val()); }); $('.quform-field-1_18').change(function () { $('#listening').text($(this).val()); }); $('.quform-field-1_19').change(function () { $('#reading').text($(this).val()); }); $('.quform-field-1_20').change(function () { $('#writing').text($(this).val()); }); $('.quform-field-1_21').change(function () { $('#speaking').text($(this).val()); }); $('.quform-field-1_11').change(function () { $('#overall').text($(this).val()); }); $('.quform-field-1_23').change(function () { $('#overall_band').text($(this).val()); }); }); // End Yes // Start No jQuery(function ($) { var calculate = function () { var total = 0; var listeningvalue = 0; var readingvalue = 0; var targetvalue = 0; var listening = $('.quform-field-6_18:checked').val(); if (listening && $.isNumeric(listening)) { total += parseFloat(listening); listeningvalue = parseFloat(listening); } var reading = $('.quform-field-6_19:checked').val(); if (reading && $.isNumeric(reading)) { total += parseFloat(reading); readingvalue += parseFloat(reading); } var target = $('.quform-field-6_22:checked').val(); if (target && $.isNumeric(target)) { targetvalue += parseFloat(target); } var writing = listeningvalue + readingvalue writing = writing / 2 var country = $('.quform-field-6_91:checked').val(); writing = writing - country writing = Math.round(writing * 2) / 2; $('.quform-field-6_84').val(writing); $('.quform-field-6_85').val(listeningvalue); total += listeningvalue + writing //Round total to either .0 or .5 // total = Math.round(total * 2) / 2; // Display the result to the user $('#form-total').text('Total ' + total); // Total field $('.quform-field-6_83').val(total); var estoverall = 0; estoverall = total / 4; estoverall = Math.round(estoverall * 2) / 2; $('.quform-field-6_86').val(estoverall); var gap = 0; var gap = targetvalue - estoverall; if(gap < 0){ gap = 0; } $('.quform-field-6_87').val(gap); }; // Calculate on page load calculate(); // Recalculate when these text input fields are changed $('.quform-field-6_18, .quform-field-6_19, .quform-field-6_22, .quform-field-6_91').on('click', calculate); }); // End No
January 15, 2019 at 11:20 am #28231AllySupport StaffYou don't have permission to view this content. Please log in or register and then verify your purchases to gain access.
- This reply was modified 5 years, 2 months ago by Ally. Reason: Added parseFloat
January 15, 2019 at 3:44 pm #28236aaron1988ParticipantHi Ally,
Forgot to say the writing field is not a checkbox my mistake so would i just change the top var writing to be:
var writing = $(‘.quform-field-6_84).val();
Thanks
AaronJanuary 16, 2019 at 8:36 am #28247aaron1988ParticipantAlso in the code i sent i have comment out the stuff to do with country as we wont be used that to do the -0.5 or -0.75 now. where abouts in my code would i put the above so it calculates it correctly?
Thanks
AaronJanuary 21, 2019 at 11:37 am #28285aaron1988ParticipantSorry to reply to post Ally wondered if you had an update, on where i can put the code so it works as expected – the new piece 🙂
Thanks
AaronJanuary 21, 2019 at 4:44 pm #28289AllySupport StaffYou don't have permission to view this content. Please log in or register and then verify your purchases to gain access.
January 21, 2019 at 4:57 pm #28290aaron1988ParticipantOk Ally,
Let me have a thing and try and get explanation to you so you know what i am trying to accomplish.
Thanks
AaronJanuary 22, 2019 at 10:44 am #28306aaron1988ParticipantHi Ally,
So this is what i need to do 🙂
i have 2 checkboxes :).
Listening and Reading so the first calculation is this we need to get the score for “Writing” which is not present except but will be a hidden field.
So the calculation i need to do is: if overall band score is <= 6.5 do -0.5 if its > 6.5 -0.75
Now for the second calculation i need to do:
Listening + Reading, the listening = speaking.
then: Listening + Reading + Writing + Speaking
Overall Band Score = Listening + Reading + Writing + Speaking / 4I going to try and work on it, but would be good to have some pointers 🙂 as i am new to javascript so learning as i go.
Also i appreciate all your help.
Thanks
AaronJanuary 25, 2019 at 4:37 pm #28333AllySupport StaffYou don't have permission to view this content. Please log in or register and then verify your purchases to gain access.
- AuthorPosts
- You must be logged in to reply to this topic.