0

A quick question: why does using a variable name after event.target return 'undefined'? I've tried event.target.getElementById(itemCode), which returns undefined, but event.target.mushroomSoup.value (one of the input ids) works fine.

Is there any way that I can get around this problem and use itemCode in place of the actual id?

    [...]
    var startersList = [];
    startersList = MenuItems.find({item_category:"starters"},{fields:{shortcode:1}).fetch();
    for (var x in startersList) {
      var itemCode = startersList[x].shortcode;
      console.log(itemCode); //e.g. mushroomSoup
      var qtyRequested = event.target.mushroomSoup.event;
      console.log(qtyRequested); //returns form input value
      var qtyRequested2 = event.target.itemCode;
      console.log(qtyRequested2); //returns undefined
    }
Corrax
  • 15
  • 5
  • 1
    please use a fiddle to explain the exact situation. the question is vague – Pratik May 12 '15 at 17:44
  • @Pratik The question here is why does `event.target.itemCode` returns `undefined` while `itemCode === 'mushroomSoup'` and `event.target.mushroomSoup` is defined. Thus, it is a duplicate of many questions about object property access using strings. – Kyll May 12 '15 at 17:53
  • Right. Sorry about that. I did come across the question but didn't make the connection then. So if I understand it correctly, "something.itemCode" refers to the itemCode property of "something", hence undefined, whereas "something[itemCode]" will "unpack" the string in itemCode to mean "something.mushroomSoup", for instance. Is that correct? – Corrax May 12 '15 at 18:11

0 Answers0