-2

I wanna change formula of javascript with input file.. How can I get "1725" from that code?

aaa = 500;
bbb = 350;
ccc = 25;
cal = document.getElementById("hesap").value;
document.getElementById("calc").value  =  cal;
<input id="hesap" value="(aaa + bbb)*2 + ccc">
<br>
<br>
<input id="calc">

I want my code to behave like ;

aaa = 500; bbb = 350; ccc = 25; 
document.getElementById("calc").value =  (aaa + bbb)*2 + ccc;
May
  • 23
  • 6
  • What do you mean by input file? – Tushar Shahi Jul 03 '21 at 16:51
  • I am editng question – May Jul 03 '21 at 16:52
  • You should write a [function](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function) to change the value... Actually, the input `value` is a string. – Louys Patrice Bessette Jul 03 '21 at 16:53
  • The *"I want my code to behave like..."* part of your question is your solution. Why do you want to have the calculation in the value attribute? – Louys Patrice Bessette Jul 03 '21 at 16:56
  • :) I know.. but this is only for an example short part of my needs.... I need to take formulation from input file – May Jul 03 '21 at 16:57
  • when I change formulation with input value I wanna get other result. – May Jul 03 '21 at 16:58
  • So explain **why**... Your question actually is an [XY problem](https://meta.stackexchange.com/a/66378/941514). I suggesst you to write a new question with your real issue and attempts... This one is likely not to be re-opened. – Louys Patrice Bessette Jul 03 '21 at 16:59
  • cause I need.. to long explanation... just need "input value" put there as an formula content. – May Jul 03 '21 at 17:01
  • @LouysPatriceBessette please check for answer.. so maybe it will be more clear – May Jul 03 '21 at 17:07
  • The answer you accepted is a super bad practice... Even if it works. Read [Why is using the JavaScript eval function a bad idea?](https://stackoverflow.com/questions/86513/why-is-using-the-javascript-eval-function-a-bad-idea) – Louys Patrice Bessette Jul 03 '21 at 17:10
  • @LouysPatriceBessette . thanks for your helps but I need to make a formula for my customer , he need to write down formula of product and than he will see what he needs etc.. how can I give him something like that ? – May Jul 03 '21 at 19:08
  • That is impossible to answer not knowing what `aaa`, `bbb` and `ccc` really is. – Louys Patrice Bessette Jul 03 '21 at 22:15

1 Answers1

1

Though it is not generally not recommended, you could use eval.

let aaa = 500,
    bbb = 350,
    ccc = 25,
    cal = document.getElementById("hesap").value;
document.getElementById("calc").value = eval(cal);
<input id="hesap" value="(aaa + bbb)*2 + ccc">
<br>
<br>
<input id="calc">
Unmitigated
  • 76,500
  • 11
  • 62
  • 80