Note that there are some explanatory texts on larger screens.

plurals
  1. POgetElementById Not Working Returns NULL
    text
    copied!<p>I'm trying to pull a dynamic page value as a JS VAR. It returns NaN. Not sure why.</p> <pre><code>function calculate(inputString) { var tendered=inputString; var curTotal=document.getElementById("total2"); x=tendered-curTotal; y=(Math.round(x * 100) / 100).toFixed(2); if (y &gt; 0) $(".submit").show(); if (y &lt; 0) { y="&lt;font color='red'&gt;Customer Still OWES: ".concat(y.replace('-','')).concat("&lt;/font&gt;"); $(".submit").hide(); } $('#change').html(y); document.getElementById('changeowed').value = y; </code></pre> <p>}</p> <p>The function is called onkeyup entering the amount tendered.</p> <p>HTML:</p> <pre><code>&lt;div class="overlay"&gt; &lt;div class="border"&gt; &lt;div class="cashform"&gt; &lt;center&gt; &lt;h1&gt;Cash Payment&lt;/h1&gt;&lt;br&gt; &lt;form name="cash" action="sale.php" method="post"&gt; &lt;h2&gt;Amount Due: &lt;font color="green"&gt;&lt;b&gt;&lt;div id="total2"&gt;&lt;/div&gt;&lt;/b&gt;&lt;/font&gt;&lt;/h2&gt; &lt;h2&gt;Amount Tendered: &lt;input type="text" id="tendered" name="tendered" size="10" onkeyup="calculate(this.value)"&gt;&lt;/h2&gt; &lt;h2&gt;Change Owed&lt;font color="green"&gt;&lt;b&gt;&lt;div id="change"&gt;&lt;/div&gt;&lt;/b&gt;&lt;/font&gt;&lt;/h2&gt; &lt;br&gt;&lt;br&gt;&lt;br&gt; &lt;input type="hidden" name="action" value="cash" /&gt; &lt;input type="hidden" name="total" value="&lt;?php echo $total; ?&gt;" /&gt; &lt;input type="hidden" id="changeowed" name="changeowed" /&gt; &lt;input type="hidden" name="sid" value="&lt;?php echo $sid; ?&gt;" /&gt; &lt;input type="submit" name="submit" value="Submit" class="submit"&gt; || &lt;input type="button" value="Cancel" class="cancel"&gt; &lt;/form&gt; &lt;/center&gt; &lt;/div&gt; &lt;/div&gt; </code></pre> <p></p> <p>I have tried with the JS above and below the HTML.</p> <p>I say the value (element) is dynamic because it changes based on the transaction (page is not reloaded when scanning items into the sale or deleting items from the sale). Adding or deleting items to or from the sale calls a JS POST function.</p>
 

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