• Register
Welcome to the in5 Answer Exchange! This a place to get answers about in5 (InDesign to HTML5) and make requests.
0 votes
I'm a horrible developer, but I want to figure this out.

I'm trying to create an IN5 iPad tool that will take in user data via the form fields and do a few basic calculations for a sales tool. Calculating things like Total cost of ownership for a product over several years. -- most of the math is pretty basic...

What I'm not clear about is how  a javascript.js file is triggered. Is it run on app launch, or run with each field-interaction. Is it always running/checking for input? Or do you have to run it on pageload, or enterfield events?

Here's a link to my two-page indesign test file. and a sample script I got from Justin earlier.

https://app.box.com/s/wms2lvvvadf89mxpjm1xqsegr8fwa9df

---

(I'm also taking a intro course on Javascript (and varients) on Udemy so I'm familiar with syntax. I've got a 3rd grade level skill in C# from previous Unity projects.)
asked in how_to by (320 points)
    

1 Answer

0 votes
 
Best answer

Creating your own custom calculator is a pretty big task. You might see if there is a jQuery plugin out there that might simplify the process for you.

Here are some basic primer notes to help you get started.

in5 uses a JavaScript library called jQuery, which can make the code simpler for you to write.

Your JavaScript should run once the body of the page is ready (so that elements on the page are available for you to target).

With jQuery, this is done using the Ready function. 

I like the shorthand version:

$(function(){
  //code goes here and runs when the document is ready
}

For items like the inputs in a calculator, you want to "listen" for events, like change (or a wait for a button to be pressed if you prefer), then run code based on the value inside the input. With jQuery you can listen for multiple events by adding a space between the names of the events, e.g.,

$(function(){
$('input#id-of-input').on('change keyup paste click',function(e){
    var value = $(this).val();
    //do something with value, e.g., recalculate(value); etc
 }
});

To use the input values as numbers, you'll also want to convert them from strings (text) to actual numbers (e.g., using the parseFloat function).

If you want to see a practical examples of jQuery, check out these two courses that I did for Lynda (Linkedin Learning):

Hope that helps!
answered by (179k points)
selected by
I think the jQuery library might be what I'm looking for. -- already found one promising option, but I'll poke around and share what I found that worked for me.

https://www.jqueryscript.net/other/jQuery-Plugin-For-Calculation-Form-Calculation-Table-Calx.html