Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>When using <code>typeof</code> in javascript, it's generally a good idea to compare the results with a triple equals comparator So instead of using <code>!=</code> use <code>!==</code></p> <p>Also in javascript, if you pass an object whose value is <code>undefined</code> into a conditional statement it will evaluate to false. <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Boolean" rel="nofollow">If value is omitted or is 0, -0, null, false, NaN, undefined, or the empty string (""), the object has an initial value of false.</a> This means that you could rewrite the above as follows unless you're specifically looking for the value to be exactly <code>undefined</code>:</p> <pre><code>function getTotal(){ // If optA does not exist, create the variable and assign it if (!optA) { var optA = getDDValue("optA"); // Otherwise it exists, now set it to "0" pleaseandthankyou :) } else { optA = "0"; } ... } </code></pre> <p><strong>EDIT :</strong> Based on your replies to the above answer I think this might help: The variable <code>element</code> is null. That means that no elements could not be found with an id of <code>elementID</code> and so it was set to null. Because it is null, it has no property <code>options</code>. Check to make sure that there actually is a DOM element with the id attribute equal to whatever you pass in as <code>elementID</code>.</p> <pre><code>function getDDValue(elementID){ var element = document.getElementById(elementID); var value; // if the element exists if(element){ value = element.options[element.selectedIndex].value; }else{ value = 0; } console.log(value); return parseFloat(value); } </code></pre>
 

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