0

Having issues with this code block:

var name = "";
var nutrients = {};
var tds = document.getElementById('data').getElementsByTagName('td');

name = tds[0].innerHTML;
nutrients[name].val = tds[1].innerHTML;
alert(nutrients.Energy.val);

If I take out the .val on both lines, the code works. I'm trying to dynamically create the " nutrients" abject by extracting information from a table. "Energy", and all 50 of nutrient names must have a "value" and a "unit" property. Eventually this will be a loop.

Thanks for any help

2
  • Check your error console, it tells you what happened. Commented Feb 27, 2013 at 17:37
  • nutrients["name"] is undefined when you want to assign val.. you can just to nutrients["name"] = tds[1].innerHTML and should work Commented Feb 27, 2013 at 17:42

1 Answer 1

2

When trying to assign

nutrients[name].val = tds[1].innerHTML;

the nutrients object is still empty, and nutrients["Energy"] (or whatever) will be undefined; throwing an exception when beeing assigned a property. Instead, use

nutrients[name] = {
    val: tds[1].innerHTML
};
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.