Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member

Calculating sales tax with JavaScript

From: Acrobat X: Creating Forms

Video: Calculating sales tax with JavaScript

In this form there is some secret math going on behind the scenes. If I order two of something that costs $15, my line total takes care of itself, notice the sales tax and the grand total. So I don't have to figure that out, somehow Acrobat is figuring it out for me. So there's my sales tax again. So let's see what I mean when I say there is hidden mask going on behind the scenes. When I choose my Select Object Tool, you can see that there is a hidden field here, let's see what job that hidden field has.

Calculating sales tax with JavaScript

In this form there is some secret math going on behind the scenes. If I order two of something that costs $15, my line total takes care of itself, notice the sales tax and the grand total. So I don't have to figure that out, somehow Acrobat is figuring it out for me. So there's my sales tax again. So let's see what I mean when I say there is hidden mask going on behind the scenes. When I choose my Select Object Tool, you can see that there is a hidden field here, let's see what job that hidden field has.

Its calculation is just the sum of the totals, so it's a little way station, it's just a place for Acrobat to store total so that this tax field can then incorporate that into some simple JavaScript. So we'll take a look at this and then we're going to replicate it in a minute. Essentially what's happening here is we're telling Acrobat, hey, go look in this field's subtotal, find out what value is in there. Look in this field's tax and for this field the b field, the value is going to be what you found in the subtotal times .06, 6% tax.

So again it looks like programming, it looks a little scary, but once you do it a couple times the logic of it is actually pretty simple. So I'm going to close this and we're going to go to the starting version and build all this, because there are really a couple of things that play here that you have to keep straight, you have to figure out your JavaScript, but you have to also make your calculations happen in the correct order. So I'm going to add a text field at the bottom, that's going to be that little hidden field, and I'm just going to call it subtotal, and ultimately I'm going to make this invisible, but to check my math I'm going to leave it visible initially and then hide it later.

So its calculation is pretty simple, it's just going to be the sum of all the total fields, and remember, I don't have to choose them individually total.0, .1, .2 so forth, I can just choose the family, I can just choose total and that's good enough, and then the tax is going to incorporate the JavaScript. So its calculation is going to be a custom calculation script, and remember what we're doing we're saying, hey, Acrobat, go look in that field subtotal, store that in a variable, so that you can use it in the math, so I just say variable a=this.getField essentially you're saying in this PDF, get the field that's named subtotal and look inside and see the value.

Then I'm going to identify this text field by saying variable b=this.getField("tax"), and here comes the math. The b.value, the value of this field equals, here comes the math, the a.value, whatever it found in subtotal times multiply by using asterisk the tax which is going to be 6% so that's .06, close the parenthesis and put on a Semicolon to finish it off.

So look in the subtotal field, talk to this tax field, and the value of this tax field is going to be that a.value times .06. When you click OK, if you don't get an alert, it means that you didn't miss anything up. When I close, these things are in place, but I'm willing to bet that my calculation order is a little out of whack. So when I go to Other Tasks>Edit Fields> Set Field Calculation Order, I want to make sure that grand total is last. I want to make sure that my subtotal happens first so that that tax field can performance its JavaScript on the result of that subtotal field and that seems to be in place, so it looks like we're in good shape.

So I'll OK, Close Form Editing, and then let's try it out and see if it works. So two of something that costs $15, there is my subtotal and then there is my Sales Tax. So three of something that costs $13. 29 just to make it a little more complex and Sales Tax figures itself out and so does the grand total. So the calculations are taking place in order, my Sales Tax field is working correctly, I can check that my subtotal is fine, now I can hide it. So I'm going to get my Selection Tool, double-click on Subtotal, go back to General, and now I can hide that little guy, but he can still do his job.

This sounds like a lot and when you hear JavaScript, maybe it strikes fear in your heart, but when you start taking it apart there is a certain simple logic to it, and you can use this form as an example of simple things you might have to do, if you ever have to do sales tax you already have the JavaScript in that field in the finished version of this form, so feel free to borrow it.

Show transcript

This video is part of

Image for Acrobat X: Creating Forms
Acrobat X: Creating Forms

38 video lessons · 15908 viewers

Claudia McCue
Author

 

Start learning today

Get unlimited access to all courses for just $25/month.

Become a member
Sometimes @lynda teaches me how to use a program and sometimes Lynda.com changes my life forever. @JosefShutter
@lynda lynda.com is an absolute life saver when it comes to learning todays software. Definitely recommend it! #higherlearning @Michael_Caraway
@lynda The best thing online! Your database of courses is great! To the mark and very helpful. Thanks! @ru22more
Got to create something yesterday I never thought I could do. #thanks @lynda @Ngventurella
I really do love @lynda as a learning platform. Never stop learning and developing, it’s probably our greatest gift as a species! @soundslikedavid
@lynda just subscribed to lynda.com all I can say its brilliant join now trust me @ButchSamurai
@lynda is an awesome resource. The membership is priceless if you take advantage of it. @diabetic_techie
One of the best decision I made this year. Buy a 1yr subscription to @lynda @cybercaptive
guys lynda.com (@lynda) is the best. So far I’ve learned Java, principles of OO programming, and now learning about MS project @lucasmitchell
Signed back up to @lynda dot com. I’ve missed it!! Proper geeking out right now! #timetolearn #geek @JayGodbold
Share a link to this course

What are exercise files?

Exercise files are the same files the author uses in the course. Save time by downloading the author's files instead of setting up your own files, and learn by following along with the instructor.

Can I take this course without the exercise files?

Yes! If you decide you would like the exercise files later, you can upgrade to a premium account any time.

Become a member Download sample files See plans and pricing

Please wait... please wait ...
Upgrade to get access to exercise files.

Exercise files video

How to use exercise files.

Learn by watching, listening, and doing, Exercise files are the same files the author uses in the course, so you can download them and follow along Premium memberships include access to all exercise files in the library.


Exercise files

Exercise files video

How to use exercise files.

For additional information on downloading and using exercise files, watch our instructional video or read the instructions in the FAQ .

This course includes free exercise files, so you can practice while you watch the course. To access all the exercise files in our library, become a Premium Member.

Join now Already a member? Log in

Are you sure you want to mark all the videos in this course as unwatched?

This will not affect your course history, your reports, or your certificates of completion for this course.


Mark all as unwatched Cancel

Congratulations

You have completed Acrobat X: Creating Forms.

Return to your organization's learning portal to continue training, or close this page.


OK
Become a member to add this course to a playlist

Join today and get unlimited access to the entire library of video courses—and create as many playlists as you like.

Get started

Already a member ?

Become a member to like this course.

Join today and get unlimited access to the entire library of video courses.

Get started

Already a member?

Exercise files

Learn by watching, listening, and doing! Exercise files are the same files the author uses in the course, so you can download them and follow along. Exercise files are available with all Premium memberships. Learn more

Get started

Already a Premium member?

Exercise files video

How to use exercise files.

Ask a question

Thanks for contacting us.
You’ll hear from our Customer Service team within 24 hours.

Please enter the text shown below:

The classic layout automatically defaults to the latest Flash Player.

To choose a different player, hold the cursor over your name at the top right of any lynda.com page and choose Site preferences from the dropdown menu.

Continue to classic layout Stay on new layout
Exercise files

Access exercise files from a button right under the course name.

Mark videos as unwatched

Remove icons showing you already watched videos if you want to start over.

Control your viewing experience

Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.

Interactive transcripts

Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.

Are you sure you want to delete this note?

No

Your file was successfully uploaded.

Thanks for signing up.

We’ll send you a confirmation email shortly.


Sign up and receive emails about lynda.com and our online training library:

Here’s our privacy policy with more details about how we handle your information.

Keep up with news, tips, and latest courses with emails from lynda.com.

Sign up and receive emails about lynda.com and our online training library:

Here’s our privacy policy with more details about how we handle your information.

   
submit Lightbox submit clicked
Terms and conditions of use

We've updated our terms and conditions (now called terms of service).Go
Review and accept our updated terms of service.