Skip to content

Instantly share code, notes, and snippets.

@New0
Created January 4, 2018 11:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save New0/2326747d100c2d0176d4cf044c80911f to your computer and use it in GitHub Desktop.
Save New0/2326747d100c2d0176d4cf044c80911f to your computer and use it in GitHub Desktop.
Example to add the calculation needed to Gil's form
add_action( 'wp_footer', function(){
echo "<script>
jQuery(document).ready(function() {
//CHANGE THE IDs TO MATCH YOUR FIELDS, these are frontend IDs, usually the field ID looking like fld_7683514 will be transformed as #fld_7683514_1
var textarea = '#fld_7683514_1'; //ID of the textarea we want to count words
var hidden_value = '#fld_8293824_1'; //ID of the hidden field we want to print count in value parameter
var text_field = '#fld_6154713_1';
var hidden_value_over_500 = '#fld_2306970_1'; //ID of the hiiden field that get the values of words over 500
var text_field_over_500 = '#fld_2296503_1';
var amount_field = '#fld_6492248_1'; // ID of the hidden field that gets the price for extra word over 500
var text_amount_over_500 = '#fld_4542284_1'; //ID of the field that prints the extra amount
var first_amount = '#fld_5704805_1-value'; // ID of the calculation hidden field to get the value of the calculation field
var total_over_500 = '#fld_8933801_1'; //Prints the total to a hidden field to be used by the paypal processor
var text_total_over_500 = '#fld_3176240_1'; //Prints the total to to a txt field for frontend user
jQuery(textarea).keyup(function() { word_count( textarea, hidden_value, text_field ) });
jQuery(textarea).keyup(function() { words_over_500( textarea, hidden_value_over_500, text_field_over_500 ) });
jQuery(textarea).keyup(function() { amount_over_500( hidden_value_over_500, amount_field, text_amount_over_500 ) });
jQuery(textarea).keyup(function() { amount_total( first_amount, amount_field, total_over_500, text_total_over_500 ) });
});
function word_count(field, count, text) {
var number = 0;
var matches = jQuery(field).val().match(/\b/g);
if(matches) {
number = matches.length/2;
}
jQuery(count).val( number );
jQuery(text).val( number );
}
function words_over_500(field, count, text) {
var number = 0;
var matches = jQuery(field).val().match(/\b/g);
if(matches) {
number = matches.length/2;
}
number = number - 500;
jQuery(count).val( number );
jQuery(text).val( number );
}
function amount_over_500(field, count, text) {
var number = 0;
var dime = 0.10;
var matches = jQuery(field).val();
if(matches) {
number = matches * dime;
}
jQuery(count).val( number.toFixed(2) );
jQuery(text).val( number.toFixed(2) );
}
function amount_total(first_total, over, hidden, text) {
var first = jQuery(first_total).val();
var plus = jQuery(over).val();
var number = first;
if(plus) {
number = parseFloat(first) + parseFloat(plus);
}
jQuery(hidden).val( number.toFixed(2) );
jQuery(text).val( number.toFixed(2) );
}
</script>";
}, 10 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment