Adding text to my array

Discussion in 'HTML & Website Design' started by Solong, Jun 27, 2013.

  1. #1
    So I'm working on my first site which is basically gonna be a site with a picture in the middle which randomizes if you click a button.

    I've done an array that look like this, except the images and links (obviously) aren't the ones I use.

    var myImages = new Array();

    myImages[1] = "oneee.jpg"
    myImages[2] = "twooo.jpg"
    myImages[3] = "threee.jpg"
    myImages[4] = "ffourrr.jpg"

    var myLinks = new Array();

    myLinks[1] = "link one"
    myLinks[2] = "link two"
    myLinks[3] = "link three"
    myLinks[4] = "link four"

    + I've added the randomizing code. It all works.

    But now I wanna add some text under the images and I can't figure out how. Anyone that can help me?
     
    Solved! View solution.
    Solong, Jun 27, 2013 IP
  2. GMF

    GMF Well-Known Member

    Messages:
    855
    Likes Received:
    113
    Best Answers:
    19
    Trophy Points:
    145
    #2
    Mhhh... since I don't know the rest of the code, I don't know how your randomization works.


    For my suggestion, I assume you use a random number (between 1-4) and with that number, you choose the image and link.

    Well, easiest would be to create a new array with the text for the images (match the index of the new array with the index of myImages[]) and then insert it into your randomizer.
     
    GMF, Jun 27, 2013 IP
  3. Solong

    Solong Peon

    Messages:
    20
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    1
    #3



    This is the tutorial I used.

    Yeah I was thinking about making a new text array. But I don't know how the array should look... This is what my head tells me to write.

    var myText = new Array();

    myText[1] = "Hello this is cool"
    myText[2] = "Bye bye"
    myText[3] = "Images are awesome"
    myText[4] = "Badoobie do"

    But first, I don't know if it's the right code to use. Second, I don't know how to write the code into the document.write code.
     
    Solong, Jun 27, 2013 IP
  4. #4
    Well, it's almost right, you just need to ad a semicolon at the end

    
    var myText = new Array();
    
    myText[1] = "Hello this is cool";
    myText[2] = "Bye bye";
    myText[3] = "Images are awesome";
    myText[4] = "Badoobie do";
    
    Code (markup):
    And then you could add it to the document.write like this

    
    document.write('<a href="'+myLinks[rnd]+'"><img src="'+myImages[rnd]+'" /></a><p>'+myText[rnd]+'</p>');
    
    Code (markup):
     
    GMF, Jun 27, 2013 IP
    ryan_uk likes this.
  5. Solong

    Solong Peon

    Messages:
    20
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    1
    #5
    Awesome, it worked!
     
    Solong, Jun 27, 2013 IP
  6. Solong

    Solong Peon

    Messages:
    20
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    1
    #6

    Do you know how to do if I wanna change color on the texts in the array? Can't figure out how to do it. Have tried everything I know of...
     
    Solong, Jun 28, 2013 IP
  7. GMF

    GMF Well-Known Member

    Messages:
    855
    Likes Received:
    113
    Best Answers:
    19
    Trophy Points:
    145
    #7
    You want to change the text colour? No problem.

    For simplicities sake, we are going to use inline css.

    
    document.write('<a href="'+myLinks[rnd]+'"><img src="'+myImages[rnd]+'" /></a><p style="color: red">'+myText[rnd]+'</p>');
    Code (markup):
    You see the style="color: red"? That changes the colour of the text within the <p> tags.
     
    GMF, Jul 1, 2013 IP