jquery - How do I generate a random image from a JavaScript array of variables into my HTML document? -
this have far in html , javascript pages, although not want use url in javascript file, since have images in project folder. when open html, no images pop up. not want background image.
var images = ["img/1.jpg", "img/2.jpg", "img/3.jpg", "img/4.jpg", "img/5.jpg", "img/6.jpg"]; var randomname = images[math.floor(math.random() * images.length)]; document.getelementbyid("randomimage").style.backgroundimage = "url('http://angelinawong.com/" + randomname + "')";
<!doctype html> <html> <head><title>47</title></head> <body> <div id="randomimage"/> <script type="text/javascript" src="jquery-3.2.1.min.js"></script> <script src="popup.js"></script> </body> </html>
try placing script tags after body tag, problem order html , js loaded.
<!doctype html> <html> <head><title>47</title></head> <body> <div id="randomimage"/> </body> <script type="text/javascript" src="jquery-3.2.1.min.js"></script> <script src="popup.js"></script> </html>
Comments
Post a Comment