Place a calculated value from javascript into an input field

I’m doing an assignment and need guidance on an order form I created.
In my screenshot below, I want to click the calculate button which will calculate the $450 price to the selected value on the dropdown list. Then place the result in the total input field at the bottom.

I’m not sure how to link it properly


HTML code:

<div id="orderform">

	<script src="../../scripts/order_form.js" type="text/javascript"></script>

<form action="" method="POST" name="bcpot002" id="bcpot002">
		<p>Shallow Copper Red dish form showing distinctive qualities of this traditional reduction fired glaze. Fired to 1300 degrees</p>
		<p>Size: 50cm diameter </br> Glaze Type: Copper Red  </p>
        <label for="price">$450/each</label>
        <select id="ordernumber">
	<input type="button" id="calculate" value="Calculate" onClick="calculate()">
      <h3>Total Cost</h3>
        $<input type="text" id="totalcost" value="0" readonly size="3" />
        <input type="submit" />
		<input value="Reset" type="reset"/>


Javascript code:

function calculate() {
	var amount = document.getElementById ("bcpot002").value;
	amount = parseFloat (amount);
	var price = 450.00;
	var totalprice = amount * price;
	return totalprice;

Full code of the HTML:

You can’t access the order number like that, you’ll have to read the value of the <select> element, not the value of the <form> element (which is undefined).

Once you get the correct amount from the function, you can insert it into the readonly <input> field by setting its value.

I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (’).

1 Like