html - Javascript array and image -
okay i've asked question before didn't answer solved problem, i'll try explain better. let have array 3 items , let landed on 0 have code set display in div need image displayed right next array displayed. example have array of cars, bmw, civic, , mercedes , let have random number generator , chooses 1 bmw word displayed in div , if civic picked civic has civic logo next , if mercedes picked mercedes have logo next it. if possible want id added in i'll put css of background-image ever pic is(reasoning want add design in picture)
<!doctype html> <html> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width"> <title>js bin</title> </head> <body> <script src="https://code.jquery.com/jquery-3.1.0.js"></script> <ul id="carlist"> </ul> <button id="getrandomcar">get random car</button> </body> <script> $(document).ready( function(){ var carsarray = [{ brand : 'bmw', logo : 'link-to-logo.png', model : 'bmw x7', }, { brand : 'honda', logo : 'link-to-logo.png', model : 'civic', }, { brand : 'mercedez-benz', logo : 'link-to-logo.png', model : 'c-class sedan', }]; function randomnumbergenerator(params) { return math.round(math.random()*params); } function randomcar() { $('#getrandomcar').click( function() { var selectedcar = randomnumbergenerator(carsarray.length - 1); console.log(selectedcar); var template = '<li>'; template += 'brand: ' + carsarray[selectedcar].brand + '</br>'; template += 'logo: ' + carsarray[selectedcar].logo + '</br>'; template += 'model: ' + carsarray[selectedcar].model + '</br>'; $('#carlist').html(template); } ); } randomcar(); }); </script> </html>
try this reference.
i've created array of objects (carsarray). since it's object, can add properties think necessary.
the sample generates random number, use index carsarray. acquired object values injected dom.
Comments
Post a Comment