0

I'm definitely a newcomer to javacsript. I copied some code from another stackoverflow post to multiply the values in two input elements and put the result into a third element.

The html/javascript is:

function Calculate() {
	
	var qty = document.getElementbyId("MemberQty").value;
        alert(qty);
	if (qty!='') {
		var price = document.getElementbyId("MemberPrice").value;
		var mcost = parseint(qty) * parseint(price);
		document.getElementbyId("MemberCost").value = mcost;
	}
	else {
		document.getElementbyId("MemberCost").value = '';
	}

}
<table>
  <tr>
    <th>Qty</th>
    <th align="left">Type</th>
    <th>Price</th>
    <th>Cost</th>
  </tr>
  <tr>
    <td align="center">
      <input type="text" size="3" style="text-align:right;" name="MemberQty" id="MemberQty" autocomplete="off" />
    </td>
    <td>Member tickets</td>
    <td>$
      <input type="text" size="5" value="20" name="MemberPrice" id="MemberPrice" readonly disabled style="background-color:#d9dfe5;border-style:none;" />
    </td>
    <td align="center">$
      <input type="text" size="5" name="MemberCost" id="MemberCost" readonly disabled style="background-color:#d9dfe5;border-style:none;" />
    </td>
  </tr>
  <tr>
    <td></td>
    <td align="right">
      <button onClick="Calculate()">Calculate</td>
    <td align="right">TOTAL</td>
    <td align="center">$
      <input type="text" size="5" id="TotalCharge" disabled readonly style="background-color:#d9dfe5;border-style:none;">
  </tr>
</table>

When Calculate() is executed, I immediately get an error "Missing required param: number". The alert dialog doesn't show so it must be the first line of the script that causes the problem.

What am I doing wrong?

Thanks, Pete

4
  • It's parseInt not parseint. Js is case sensitive. Commented Mar 3, 2016 at 20:24
  • 1
    Two things. First, you're getting an error because you cannot put <script> tags in the javascript section of the code snippet. Second, you have two spelling errors in your javascript. getElementbyId should be getElementById and parseint should be parseInt. Methods in javascript are always camelcased like that Commented Mar 3, 2016 at 20:24
  • 1
    If I click your Calculate button, I get the error TypeError: document.getElementbyId is not a function (this is because the b in by is lowercase). Could you show us code that actually produces the error you say it does? Commented Mar 3, 2016 at 20:28
  • Thanks for the corrections everyone. After fixing the typos, all works fine. I've also added the changes suggested by Sandro. Commented Mar 4, 2016 at 18:08

1 Answer 1

1

Some flaws in your code:

turn all occurrances of

document.getElementbyId

into

document.getElementById

change

parseint(qty)

into

parseInt(qty,10)

parseInt was mispelled: this is the correct syntax for 10-base input numbers.

change

parseint(price)

into

parseFloat(price)

same as before, but you can handle prices with decimals too

change

<button onClick="Calculate()">

into

<button onclick="javascript:Calculate()">

Take care to announce the language interpreted by the given event (onclick, here). I would also suggest to write

qty = parseInt(qty,10);
if ( isNaN(qty) ) qty = 0 ;

right after

var qty = document.getElementbyId("MemberQty").value;

At last, turn

if (qty!='')

into

if (qty >= 0)

Hope this helps and happy coding !

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.