Skip to main content
Inspiring
February 6, 2024
Answered

Help with javascript that add state sales tax amount to text box

  • February 6, 2024
  • 1 reply
  • 725 views

I've been piecing together a javascript that is supposed to add the state sales tax (in decimal format) to my text box named "Tax_Rate". The value that is entered in the box is based on the abbreviated state name entered into the "State" text box. I've tried using Switch and IF for the script and both had no errors. I can not get this script to run in my form though, what am I missing? Please help, I'm pulling my hair out!

if

function addTaxRate() {
    var taxRate = 0;
    var State = this.getField("State");
    if (State == "AZ") {taxRate = .0925}
        else{taxRate = 0}
    if (State == "CA") {taxRate = .0815}
        else{taxrate = 0}
    if (State == "ID") {taxrate = 0.060}
        else{taxrate =0}
    if (State == "IN") {taxrate = 0.070}
        else{taxrate-0}
    if (State == "MI") {taxrate = .060}
        else{taxrate = 0}
    if (State == "NJ") {taxrate = .0662}
        else{taxrate = 0}
    if (State == "PA") {taxrate = .070}
        else{taxrate = 0}
    if (State == "TX") {taxrate = .0925}
        else {taxrate = 0}
    if (State == "UT") {taxrate = .0710}
        else {taxrate = 0}
    if (State == "WA") {taxrate = .0860}
        else {taxrate = 0}
    event.value = Tax_Rate;
}
 
 
Switch
function addTaxRate() {
    var taxRate = 0;
 
    switch (this.getField("State_0").valueAsString) {
        case 'AZ':
          taxRate = 0.0920;
          break;
        case 'CA':
          taxRate = 0.0815;
          break;
        case 'ID':
          taxRate = 0.060;
          break;
        case 'IN':
          taxRate = .070;
           break;
         case 'MI':
           taxRate = .060;
            break;
          case 'NJ':
            taxRate = .0662;
             break;
          case 'PA':
           taxRate = .0700;
           break;
          case 'TX':
            taxRate = .0925;
             break;
           case 'UT':
             taxRate = .0710;
            case 'WA':
              taxRate = .0860;
        default:
          taxRate = 0;
          break;
      }
 
    this.getField("Tax_Rate").value = taxRate;
  }
 
Correct answer glazed01

Please post the file with the script you tried.


I did try the switch script after making the changes you recommended and used it as a validation script in the State_0 box. I currently have the script you provided above in the same validation. Below is the "switch" with recommended changes

var taxRate = 0;
 
    switch (this.getField("State_0").valueAsString) {
        case 'AZ':
          taxRate = 0.0920;
          break;
        case 'CA':
          taxRate = 0.0815;
          break;
        case 'ID':
          taxRate = 0.060;
          break;
        case 'IN':
          taxRate = .070;
           break;
         case 'MI':
           taxRate = .060;
            break;
          case 'NJ':
            taxRate = .0662;
             break;
          case 'PA':
           taxRate = .0700;
           break;
          case 'TX':
            taxRate = .0925;
             break;
           case 'UT':
             taxRate = .0710;
            case 'WA':
              taxRate = .0860;
        default:
          taxRate = 0;
          break;
      }
 
    switch (event.value)

1 reply

Nesa Nurani
Community Expert
Community Expert
February 7, 2024

The scripts were close, but they have few errors, and it depends on where you use them.

For switch script, remove first line.

function addTaxRate() {

and last curly bracket and replace:

switch (this.getField("State_0").valueAsString) {

with:

switch (event.value) {

Now use that script as 'Validate' script of "State_0" field.

Or use this:

 

 

var taxRate = 0;
var State = event.value;
 if (State == "AZ") taxRate = .0925;
 if (State == "CA") taxRate = .0815;
 if (State == "ID") taxRate = 0.060;
 if (State == "IN") taxRate = 0.070;
 if (State == "MI") taxRate = .060;
 if (State == "NJ") taxRate = .0662;
 if (State == "PA") taxRate = .070;
 if (State == "TX") taxRate = .0925;
 if (State == "UT") taxRate = .0710;
 if (State == "WA") taxRate = .0860;
this.getField("Tax_Rate").value = taxRate;

 

 

glazed01Author
Inspiring
February 7, 2024

@Nesa Nurani the intent is to use the code in the Tax_Rate box to populate the tax based on the abbreviated state. Although, using it in the State_0 box makes more sense. The script you have above works when I use it in the State_0 text box, however, the tax rate shows as 0.00 for each state listed. Any suggestions?

 

 

Nesa Nurani
Community Expert
Community Expert
February 7, 2024

Please post the file with the script you tried.