Skip to content Skip to sidebar Skip to footer

Javascript Help Using Html And Calculator

Can someone help me to get the total price of the stock given prices below?? Using the onclick, once someone puts in the number of stocks they want to buy, how do I go about gettin

Solution 1:


I would suggest storing the price information as a numeric value somewhere, like in hidden input fields or data- attributes on the table cells, and create IDs on those elements that you can associate with the inputs for stock purchases. Then it's just a matter of doing some simple math. Here's an example using hidden inputs:

<table><tr><td><b> SHARE PRICE</b></td><td>$43.93</td><td>$43.87</td><td>$26.33</td></tr></table><h3>Important information that you should know about these stocks: </h3><ul><li>0.1% of the trade value if the total trade value is less than $10,000</li><li>0.08% of the trade value if the total trade value is greater than or equal to $10,000</li><li>The minimum commission is $5</li></ul><formname="calculator"><inputtype="hidden"id="price1"value="43.93" /><inputtype="hidden"id="price2"value="43.87" /><inputtype="hidden"id="price3"value="26.33" /><p> Enter the number of Oracle Corporation stocks you wish to purchase!: <inputtype="text"id="input1"></p><br><p> Enter the number of Microsoft Corporation stocks you wish to purchase!: <inputtype="text"id="input2"></p><br><p> Enter the number of Symantec Corporation stocks you wish to purchase!: <inputtype="text"id="input3"></p><br><inputtype="button"value="Add!"onclick="javascript:sumUp()" /></form><scripttype="text/javascript">functionsumUp() {
        var total = (document.getElementById("price1").value * document.getElementById("input1").value) + (document.getElementById("price2").value * document.getElementById("input2").value) + (document.getElementById("price3").value * document.getElementById("input3").value)
        alert("Your total is: $" + total);
    }
</script>

Here's the code for putting the total into a textbox. This would go at the end of your form and replace the <script> block from the first example.

<p>Your total is: $<inputtype="text"id="total" /></p><scripttype="text/javascript">functionsumUp() {
        var total = (document.getElementById("price1").value * document.getElementById("input1").value) + (document.getElementById("price2").value * document.getElementById("input2").value) + (document.getElementById("price3").value * document.getElementById("input3").value)
        document.getElementById("total").value = total;
    }
</script>

Post a Comment for "Javascript Help Using Html And Calculator"