title: JavaScript: Can I declare a variable by querying which function is called? (Newbie)
body: <p>I'm working with an existing JavaScript-powered cart module that I am trying to modify. I do not know JS and for various reasons need to work with what is already in place.</p>
  
  <p>The text that appears for my quantity box is defined within an existing function:</p>
  
  <pre><code>function writeitems() {
  
  var i;
  for (i=0; i&lt;items.length; i++) {
      var item=items[i];
      var placeholder=document.getElementById("itembuttons" + i);
      var s="&lt;p&gt;";
  
      // options, if any
      if (item.options) {
          s=s+"&lt;select id='options"+i+"'&gt;";
          var j;
          for (j=0; j&lt;item.options.length; j++) {
              s=s+"&lt;option value='"+item.options[j].name+"'&gt;"+item.options[j].name+"&lt;/option&gt;";
          }
          s=s+"&lt;/select&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;";
      }
  
  
  
  // add to cart
  
      s=s+"Quantity: &lt;input id='quantity"+i+"' value='1' size='3'/&gt; ";
  
      s=s+"&lt;input type='submit' value='Add to Cart' onclick='addtocart("+i+"); return false;'/&gt;&lt;/p&gt;";
      }
  
  
      placeholder.innerHTML=s;
  }
  refreshcart(false);
  }
  </code></pre>
  
  <p>I have two different types of quantity input boxes; one (donations) needs to be prefaced with a dollar sign, and one (items) should be blank.</p>
  
  <p>I've taken the existing additem function, copied it, and renamed it so that there are two identical functions, one for items and one for donations.  The additem function is below:</p>
  
  <pre><code>function additem(name,cost,quantityincrement) {
  if (!quantityincrement) quantityincrement=1; 
  var index=items.length;
  items[index]=new Object;
  items[index].name=name;
  items[index].cost=cost;
  items[index].quantityincrement=quantityincrement;
  
  document.write("&lt;span id='itembuttons" + index + "'&gt;&lt;/span&gt;");
  
  return index;
  
  }
  </code></pre>
  
  <p>Is there a way to declare a global variable based on which function (additem or adddonation) is called so that I can add that into the writeitems function so display or hide the dollar sign as needed? Or is there a better solution?</p>
  
  <p>I can't use HTML in the body of the cart page because of the way it is currently coded, so I'm depending on the JS to take care of it.</p>
  
  <p>Any help for a newbie is welcome. Thanks!</p>
  
  <p>UPDATE 3/22:
  Ok, I've edited the .js file as such:
          var items=new Array;
      var cart=new Array;
      var isDonation=false;</p>
  
  <pre><code>function additem(name,cost,quantityincrement) {
  var isDonation=false;
  if (!quantityincrement) quantityincrement=1; 
  var index=items.length;
  items[index]=new Object;
  items[index].name=name;
  items[index].cost=cost;
  items[index].quantityincrement=quantityincrement;
  
  document.write("&lt;span id='itembuttons" + index + "'&gt;&lt;/span&gt;");
  
  return index;
  }
  
  function adddonation(name,cost,quantityincrement) {
  var isDonation=true;
  if (!quantityincrement) quantityincrement=1; 
  var index=items.length;
  items[index]=new Object;
  items[index].name=name;
  items[index].cost=cost;
  items[index].quantityincrement=quantityincrement;
  
  document.write("&lt;span id='itembuttons" + index + "'&gt;&lt;/span&gt;");
  
  return index;
  }
  
  ...
  
  function writeitems() {
  
  var i;
  for (i=0; i&lt;items.length; i++) {
      var item=items[i];
      var placeholder=document.getElementById("itembuttons" + i);
      var s="";
  
      // options, if any
      if (item.options) {
          s=s+"&lt;select id='options"+i+"'&gt;";
          var j;
          for (j=0; j&lt;item.options.length; j++) {
              s=s+"&lt;option value='"+item.options[j].name+"'&gt;"+item.options[j].name+"&lt;/option&gt;";
          }
          s=s+"&lt;/select&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;";
      }
  
  
  
  // add to cart
      if (!isDonation) {
      s=s+"&lt;input id='quantity"+i+"' size='3' type='hidden'/&gt;";
      s=s+"&lt;input type='submit' value='Add to Cart' onclick='addtocart("+i+"); return false;'/&gt;&lt;br /&gt;&lt;br /&gt;";
      }
      else {
  
      s=s+"$&lt;input id='quantity"+i+"' size='3'/&gt;.00 ";
      s=s+"&lt;input type='submit' value='Add to Cart' onclick='addtocart("+i+"); return false;'/&gt;&lt;br /&gt;&lt;br /&gt;";
      }
  
      placeholder.innerHTML=s;
  }
  refreshcart(false);
  }
  </code></pre>
  
  <p>The resulting page isn't differentiating between the additem and adddonation functions is the output. Changing if/else statement to isDonation=true changes all output universally, and isDonation=false changes likewise, but also universally.</p>
  
  <p>I'm at a complete loss.</p>