Note that there are some explanatory texts on larger screens.

plurals
  1. POAuto calculation occurs on previous value
    primarykey
    data
    text
    <p>i have a function that increments a number, takes the total and multiplies it by a price. However, when I click the button that increments for the first time it doesn't do anything. I click the button a second time and it works. However if I then click the button that decreases, then it does the previous function (e.g. adding) then pressing it a second time will decrease the value.</p> <p>I understand that the <code>onClick="myFunction()"</code> may be in the wrong place, however I don't know where to put it. I want the total to be calculated automatically. A demo can be found at <a href="http://alpha.kentishtours.co.uk/destinations/bruges.php" rel="nofollow">http://alpha.kentishtours.co.uk/destinations/bruges.php</a></p> <p>The function that takes the value and multiplies it and calculates the total.</p> <pre><code>function myFunction() { var a = document.getElementById('adult').value; z=39; x=a*z; var c = document.getElementById('child').value; if(a &gt;= 1) { a=+30; } else { a=+39; } b=c; c=a*b d=x+c document.getElementById("total").innerHTML=d; document.getElementById("value").value = d; }` </code></pre> <p>These are the buttons that increment the number and call the function to calculate. </p> <pre><code>&lt;div class="calculator"&gt; &lt;div class="calculator-submodule input-group"&gt; &lt;h4&gt;Adult (12+)&lt;/h4&gt; &lt;button class="btn theme-btn" id="decrease" value="Decrease Value" onClick="myFunction()" &gt;-&lt;/button&gt; &lt;input type="text" id="adult" value="1" class="form-control input-usmall" min="0" disabled&gt; &lt;button class="btn theme-btn" id="increase" value="Increase Value" onClick="myFunction()" &gt;+&lt;/button&gt; &lt;/div&gt; &lt;div class="calculator-submodule input-group"&gt; &lt;h4&gt;Child (2-11)&lt;/h4&gt; &lt;button class="btn theme-btn" id="decreasec" value="Decrease Value" onClick="myFunction()" &gt;-&lt;/button&gt; &lt;input type="text" id="child" value="0" class="form-control input-usmall" min="0" disabled&gt; &lt;button class="btn theme-btn" id="increasec" value="Increase Value" onClick="myFunction()" &gt;+&lt;/button&gt; &lt;/div&gt; &lt;div class="calculator-submodule"&gt; &lt;span class="pound"&gt;£&lt;/span&gt; &lt;span id="total"&gt;39&lt;/span&gt;&lt;span class="pound"&gt;.00&lt;/span&gt; &lt;/div&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload