Thunderforge Posted November 9, 2016 Share Posted November 9, 2016 I want to randomly generate an src path for an image, for example; src = <var1>/<var2>.gif I'm sure it can be done, but not sure at what level it would go in. This is what I have; var huePick =[{text:'light',image:'light'},{text:'dark',image:'dark'}]; var colourPick =[{text:'green',image:'green.gif'},{text:'red',image:'red.gif'}]; function myFunction() { var hueChosen= huePick [Math.floor(Math.random()*huePick.length)]; var colourChosen= colourPick [Math.floor(Math.random()*colourPick.length)]; document.getElementById("thisResult").src = hueChosen.image / colourChosen.image; } The id called 'thisResult' I want to be able to use as a url I can drop into an image tag. Which would look like this; dark/green.gif and locate the relevant image in the relevant folder. (I know I could reference them all in one folder with one long variable but I have reasons why not to) How do I arrange the getElementById string and implement it into an image tag? Does any of that make sense? (Obviously I would have a corresponding file structure with different gifs in the light folder to those in the dark folder, but with same names.) Link to comment Share on other sites More sharing options...
justsomeguy Posted November 9, 2016 Share Posted November 9, 2016 You use the + operator to concatenate strings. Scroll down to the section on string operators: http://www.w3schools.com/js/js_operators.asp Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now