Use JavaScript with calculate

Hi all, I’m new to WIX platform.
I want to make a page to let visitor input some number then let element (ans1 and ans2) show the results automatically (without “Do” button). But I can’t find a way to do it, so I made a “Do” button ver…

The original code (works fine in local HTML file)

<script language="javascript">
function showme(){
  var x1 = document.getElementById('x1').value;
  var x2 = document.getElementById('x2').value;
  var x3 = document.getElementById('x3').value;
  var x7 = document.getElementById('x7').value;
  var x5 = document.getElementById('x5').value;
  var x6 = document.getElementById('x6').value;  
  var x8 = document.getElementById('x8').value;  
  var x11 = document.getElementById('x11').value;
  var x11 = document.getElementById('x11');       
  var ans1 = document.getElementById('ans1');
  var ans2 = document.getElementById('ans2');
  x11.value=(x8/100*29);
  ans1.value=(x1*1+x2*2+x3*3)*x7*4+((x11.value)*5+x5*6+x6*7);
  ans2.value=(x1*0.25+200)
  setTimeout("showme()",1000);
}
</script>

I tried to translate into WIX syntax, it looks like this:

export function showme() {
  let x1 = $w("#x1").value;
  let x2 = $w("#x2").value;
  let x3 = $w("#x3").placeholder;
  let x7 = 0.281;
  let x5 = $w("#x5").placeholder;
  let x6 = $w("#x6").value;
  let x8 = $w("#x8").value;
  let x11 = $w("#x11").value;
  let ans1 = $w("#ans1").value; //SYS shows "ans1 is unread" don't know why :(
  if ($w("#x8").value > 0) {
    x11 = x8 / 100 * 29;
    $w("#x11").value = x11;
  } else {
    $w("#x11").placeholder = "err msg";
  }
  $w("#ans1").value = parseInt((x1 * 1 + x2 * 2 + x3 * 3) * x4 * 4 + ((x11) * 5 + x5 * 6 + x6 * 7), 10);
  //ans2.value=(x1*0.25+200) //skip ans2 for now
  setTimeout("showme()",1000);
}

$w.onReady(function () {
  //
});

export function Do_click(event) {
  showme();
}

It seems work, but I keep getting error message “Uncaught ReferenceError: showme is not defined” every time I press the DO button.

anyone have any idea? or any better way to do same thing, please?

Can you provide the formula you want to calculate ?

Example: (input 1) + (input 2)

Hi shan,

thanks for reply.
Sure

-User input element ID : x1, x2, x6, x8,
-Fix element ID (read only) : x3, x5, x7
-Formula :
x11(once x8 was input) = x8/10029
ans1 = (x1
1+x22+x33)x74+(x115+x56+x67)
ans2 = x1
0.25+200

Hi!

I believe problem is here -
setTimeout(“showme()”,1000);

Read about this function here - Window setTimeout() Method

I’m not usre what are you trying to achieve with this function, but in any case you need to pass there function, but you’re trying to pass there string - “showme()”.

Should be smth like - setTimeout( showme(),1000);

Oops it’s embarrassing didn’t notice that, finally no more error :smiley:
Thanks Mikhail Spirin !!