javascript - JS arrays - how to build an array -


    var adlist = new array();     adlist[0]["img"] = 'http://www.gamer-source.com/image/media/screenshot/thumbnail/489.jpg';/*     adlist[0]["h3"] = 'product title2';     adlist[0]["button"]["link"] = '#link-url';     adlist[0]["button"]["text"] = 'buy now';     adlist[0]["h3"] = 'asdfasdfasdf';      adlist[1]["img"] = 'http://www.gamer-source.com/image/media/screenshot/thumbnail/489.jpg';     adlist[1]["h3"] = 'product title2';     adlist[1]["button"]["link"] = '#link-url';     adlist[1]["button"]["text"] = 'buy now';     adlist[1]["h3"] = 'asdfasdfasdf'; 

i'm getting error adlist[0] undefined, can not define arrays this? have specify adlist[0] array before assign variables it?

the problem you're trying refer adlist[0] hasn't had value assigned it. when try access value of adlist[0]['img'], happens:

adlist           -> array adlist[0]        -> undefined adlist[0]["img"] -> error, attempting access property of undefined. 

try using array , object literal notation define instead.

adlist = [{     img: 'http://www.gamer-source.com/image/media/screenshot/thumbnail/489.jpg',     h3: 'product title 1',     button: {         text: 'buy now',         url: '#link-url'     } }, {     img: 'http://www.gamer-source.com/image/media/screenshot/thumbnail/489.jpg',     h3: 'product title 2',     button: {         text: 'buy now',         url: '#link-url'     } }]; 

or can define adlist[0] , adlist[1] separately , use old code:

var adlist = new array(); adlist[0] = {}; adlist[1] = {}; adlist[0]["img"] = 'http://www.gamer-source.com/image/media/screenshot/thumbnail/489.jpg'; ... etc etc 

Comments

Popular posts from this blog

Add email recipient to all new Trac tickets -

400 Bad Request on Apache/PHP AddHandler wrapper -

php - Change action and image src url's with jQuery -