Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    New Coder
    Join Date
    Dec 2011
    Posts
    59
    Thanks
    51
    Thanked 1 Time in 1 Post

    trying to display image using array value

    trying to display an image using an img tag
    which uses the array value as the image path



    here is where i set up the array called "img_path" in the head

    <script type="text/javascript">
    var img_path = [
    'images/west/Home.png',
    'images/west/Cold.png',
    'images/west/Ocean.png',
    'images/west/Beach.png'
    ];
    </script>


    later, in the body, i'm trying to use the array value to splash the image on the web page, like this

    <img src="img_path[0]" width="250" height="250">


    the result splashes a 250x250 image area on the webpage but not the image (?). why not the image ?



    i tested the value of "img_path[0]" and it prints out correctly as 'images/west/Home.png'



    i even cut/pasted this code in and the image splashed out just fine.
    <img src="images/west/Home.png" width="250" height="250">



    not sure why this won't work?
    anyone have any suggestions?

    thanks,
    Paul

    weather in Kalispell, MT this morning........not to bad, about 28deg.
    Merry Christmas and Happy New Year to all.

  • #2
    Senior Coder xelawho's Avatar
    Join Date
    Nov 2010
    Posts
    3,020
    Thanks
    56
    Thanked 566 Times in 563 Posts
    I'm thinking that this bit:
    Code:
    <img src="img_path[0]" width="250" height="250">
    is in your html, but img_path[0] is javascript...

    I believe you need to set the img src in your html using html code and then change it using a js function in your js

    or if not, show a bit more code.

  • #3
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,314
    Thanks
    203
    Thanked 2,565 Times in 2,543 Posts
    Quote Originally Posted by xelawho View Post
    I'm thinking that this bit:
    Code:
    <img src="img_path[0]" width="250" height="250">
    is in your html, but img_path[0] is javascript...

    I believe you need to set the img src in your html using html code and then change it using a js function in your js
    Simple example:-

    Code:
    <script type="text/javascript">
    var img_path = ["One.jpg","Two.jpg"];
    </script>
    
    <img src= "One.jpg"  name = "photoslider" width="250" height="250">
    <br>
    
    <script type = "text/javascript">
    function change () {
    document.photoslider.src = img_path[1];
    }
    </script>
    
    <input type = "button" value = "Change" onclick = "change()">

    That will dampen the half-time cup of tea. Commentator BBC Radio 5 Live

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • Users who have thanked Philip M for this post:

    Paul Williams (12-26-2011)

  • #4
    Senior Coder xelawho's Avatar
    Join Date
    Nov 2010
    Posts
    3,020
    Thanks
    56
    Thanked 566 Times in 563 Posts
    or to get it really clickin':

    Code:
    <script type = "text/javascript">
    var j=0;
    function change () {
    document.photoslider.src=img_path[j++%img_path.length]
    }
    </script>

  • Users who have thanked xelawho for this post:

    Paul Williams (12-26-2011)

  • #5
    New Coder
    Join Date
    Dec 2011
    Posts
    59
    Thanks
    51
    Thanked 1 Time in 1 Post

    thank you

    Xelawho & Phillip M.

    Hmmm, Very interesting.

    thank you.
    Well, I was close.

    Back to the JS textbook, more study for Grasshopper.
    Many many thanks.

    Phillip,
    My very good friend, Lloyd Brown, is your Olympic Archery National Coach.
    The next Olympics are at home for you.
    History has proven that the host country in every Olympics dating back to 1984 has produced a GOLD Medalist in Archery (except for Greece 2004, the girl from Greece advanced all the way to the quarter finals and lost to a Korean, ah nuts, so close……the crowd was going wild, I know cuz I was there .
    Good for you’all. I certainly hope it happens for your country and for my friend.

    That will top-off the half time cup of tea.
    Cheers,
    Paul

  • #6
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,314
    Thanks
    203
    Thanked 2,565 Times in 2,543 Posts
    Quote Originally Posted by Paul Williams;1174366Phillip,
    My very good friend,[B
    Lloyd Brown[/B], is your Olympic Archery National Coach.
    "Funding is to be cut for sports where targets have been missed, such as archery". - BBC Scotland News.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •