0

I'm trying to assign a reference to a DOM element to a property when declaring an object but it won't work like this:

var myMedia = {
 "v" : document.getElementById("video"),
 "a" : document.getElementById("audio"),
 "g" : "myMediaGroup",
};

...myMedia.v returns null, but if I assign it like this:

myMedia.v = document.getElementById("video");

...it works great.

Using object-literal, can I only assign strings to properties? How could I do it differently?

Johan
  • 43
  • 4
  • 4
    Are you sure your DOM is loaded when defining the object literal? – Sebastian Simon Aug 23 '15 at 19:53
  • Don't put a comma after the last element of your object initializer:`"g" : "myMediaGroup"` – rplantiko Aug 23 '15 at 19:58
  • @rplantiko There is no problem with having a trailing commas in an Object litteral or an Array. Actually, it helps prevent errors when adding new properties/elements later. http://ecma262-5.com/ELS5_HTML.htm#Section_11.1.5 – blex Aug 23 '15 at 20:00
  • @rplantiko the trailing comma will be ignored by the parsing engine. – BenM Aug 23 '15 at 20:00
  • @Xufox, you were right. I'm too tired right now... – Johan Aug 23 '15 at 20:01

1 Answers1

0

My bad. The script tag was in the head of the document and not below the audio and video tags. It just couldn't find the elements :P

Johan
  • 43
  • 4