I am hoping someone can help with the correct syntax to make my code a littler cleaner. I have code that takes a voltage rating input field and compares it to some ranges in combination with the value from another field, then fills the background color and sets a 3rd field to PASS or F. The section of code doing that comparison stays the same and could be reused many time in my form, but I don't know the correct syntax to call that code and pass the variables that would be different to it. It's something like this...
$(document).ready(function(){
$(document).on('change', '.InsResistTest3 input', function(){
//VRating needs to reference the q# for Voltage Rating from the Nameplate Section
var VRating = parseFloat($('#q309 input').val().replace(/,/g,''));
//PFField references the field that is set to either F or PASS and is checked by the overall pass fail field formula
var PFField = $('#q345 input');
//The variables above would be different based on which VRating and PFField were being referenced.
//The code below could be called mutiple times, with different VRating and PFField variables passed to it.
if ((VRating <= 250) && ($(this).val() < 0.025)){
this.setAttribute('style', 'background-color: tomato !important')
PFField.val('F').change();
}
else if ((VRating > 250) && (VRating <= 1000) && ($(this).val() < 0.100)){
this.setAttribute('style', 'background-color: tomato !important')
PFField.val('F').change();
}
else if ((VRating > 1000) && (VRating <= 2500) && ($(this).val() < 0.500)){
this.setAttribute('style', 'background-color: tomato !important')
PFField.val('F').change();
}
else if ((VRating > 2500) && (VRating <= 5000) && ($(this).val() < 1.000)){
this.setAttribute('style', 'background-color: tomato !important')
PFField.val('F').change();
}
else if ((VRating > 5000) && (VRating <= 8000) && ($(this).val() < 2.000)){
this.setAttribute('style', 'background-color: tomato !important')
PFField.val('F').change();
}
else if ((VRating > 8000) && (VRating <= 15000) && ($(this).val() < 5.000)){
this.setAttribute('style', 'background-color: tomato !important')
PFField.val('F').change();
}
else if ((VRating > 15000) && (VRating <= 25000) && ($(this).val() < 20.000)){
this.setAttribute('style', 'background-color: tomato !important')
PFField.val('F').change();
}
else {
$(this).css('background-color', 'white')
PFField.val('PASS').change();
}
});
});
Thanks in advance for any help!