Jason Waltz
Jason Waltz

Reputation: 435

How do I use a HTML input box to set a JavaScript variable?

I am trying to write a simple one page calculator script with user input. With my semi-lack of JS knowledge I keep running into issues. I feel like what I am doing should work and is correct but I am obviously missing something here.

I would like my HTML input box's to set the variables for my JS function. I just can not figure out what I am doing wrong here.

If I could have a nudge in the right direction I would be more than thankful.

<body>
   <input type="text" id="price"=>Price</input>
   <br>
   <input type="text" id="value"=>Value</input>
   <br>
   <button onclick="sub()">Submit</button>
</body>
<script>      
   function sub() {
           
   var value = document.getElementById("value").value;
   var price = document.getElementById("price").value;
           
   var keep = value * 0.7
   var sell = keep * 0.7
   var profit = sell / 1000 * 6 - price
   var keeprate = price / keep * 1000
   var earned = profit + price
   
   
   
   document.write("Keeping: R$ ");
   document.write(keep);
   document.write("<br>");
   document.write("1k Buy Rate: $");
   document.write(keeprate);
   document.write("<br>");
   document.write("<br>");
   document.write("Selling: R$ ");
   document.write(sell);
   document.write("<br>");
   document.write("1k Buy Rate: ");
   document.write("$6.00");
   document.write("<br>");
   document.write("<br>");
   document.write("Total Cost $");
   document.write(price)
   document.write("<br>");
   document.write("Total Return $");
   document.write(earned)
   document.write("<br>");
   document.write("Net: $");
   document.write(profit);
   }
</script>

Upvotes: 0

Views: 491

Answers (1)

jriley88
jriley88

Reputation: 117

I think your main issue is your function declaration. You have no opening and closing bracket for function sub()

Add an opening and closing bracket to properly declare your function.

function sub() {

    code

}

Upvotes: 2

Related Questions