Solved - How can I used variables from JS in HTML?

How can I get an alert input variables amount and percent in scriptable JS and assign both variables to an HTML variable to make a Tax calculation and display it in a WebView.loadHTML…?

Or please, recommend me another way to do this. This is my first JS. :slightly_frowning_face:

The code:


var amountInput = new Alert();
amountInput.title = "IVU";
amountInput.message = "Type ticket amount and IVU";
amountInput.addTextField("Amount");
amountInput.addTextField("IVU");
amountInput.addAction("OK");
await amountInput.present();
console.log("Hello " + amountInput.textFieldValue(0) + " " + amountInput.textFieldValue(1));

var html = `
<!DOCTYPE html>
<html>
<head>
<h2>IVU</h2>
<p>Impuesto de ventas y uso</p>
<style>
input{
  padding: 10px;
  font: 20px Arial;
  width: 15%;
}
</style>
</head>

<body style="background-color:DarkSeaGreen; color:MidnightBlue; font-family:avenir; font-size:400%; text-align: center;">

<p id="output"></p>

<script type="text/javascript">
let percent = 11.5 // want a var here instead of 11.5 percent
let amount = 100  // want a var here instead of 100 amount
var ivu = (percent / 100)
var totalIvu = (ivu * amount).toFixed(2)
var totalAmount = Math.round(amount * (ivu + 1) * 100).toFixed(2) / 100

document.getElementById("output").innerHTML = "IVU " + percent + "%" + "<br><br>" + "Bill " + amount + "<br><br>" + "Ivu " + "$" + totalIvu + "<br><br>" + "Total " + "$" + totalAmount;
</script>

</body>
`;

WebView.loadHTML(html, null, new Size(0, 100));
Script.complete();

Can you post your code or a link to it? Your post appears to stop short of including it,

How can I include my code? Because when I copy and paste the link, it paste the code instead of link. And the code is not pp(pretty printed). And when I paste the code in this body it’s truncated. And when I do a print screen I get a message that can only load one image. And when I paste the link in title I get a message that title have a limit of characters.

Put three backticks before your code and after your code, or press the </> bar in the editor.

```
code
```

This is how I type my code.

2 Likes

Thanks sir, I finally can include my code.

Thanks Rosemary for your suggestion.

Try this

<script type="text/javascript">
let percent = ${amountInput.textFieldValue(1)}
let amount = ${amountInput.textFieldValue(0)}  
var ivu = (percent / 100)
1 Like

:star::star::star::star::star:
Oh great! Sylumer, your suggestion was the solution.

Final Output: