3

I need to detect a change in a Gravity Form field (form 5, field 215; is a number field), round the number so I can get a trailing zero if it doesn't already have one, and then return the new, rounded value back to the field. I tried to piecemeal something together using bits of other code I found, but I must be doing something wrong. I'm an absolute rookie with JavaScript.

I'm using Gravity Wiz's "Gravity Forms Custom JavaScript" plugin to insert this script only on the page with the appropriate form.

jQuery(“#gform_fields_215”).on(“change”,”#input_215″, ( function(e) {
var number = document.getElementById("input_215");
var rounded = number.toFixed(2);
document.getElementById("input_215").value = rounded;
}

What am I doing wrong? Probably everything! LOL

1 Answers1

0

I know you have already forgotten you ever asked this. but here's the solution.

jQuery('#gform_fields_215').on('change','#input_215', ( function(e) {
  var number = document.getElementById("input_3_1_5").value;
  var rounded = parseInt(number).toFixed(2);
  document.getElementById("input_215").value = rounded;
}

You have used jQuery and vanilla js syntax. if you would use only jQuery it would be done as below.

jQuery("#gform_3").on("change", "#input_3_1_5", function (e) {
  $(this).val(parseInt(this.value).toFixed(2));
});
  • element value is a string it need to be parsed into a number before adding decimal places
  • this.value can be used to get value of current element
dee
  • 2,244
  • 3
  • 13
  • 33
MavenSanK
  • 1
  • 1