SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Addict
    Join Date
    Oct 2005
    Posts
    288
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    how to use id instead of name to change image src

    Stupid question -- but those are the easiest to answer, I guess.
    All answers I've found are all too sophisticated.

    This excerpt works but is not XHTML strict compliant because of illegal use of "name"
    Code:
    function animator() {
      document.animimage.src = anims[frame].src;    <-- this is the offending javascript but it works
      frame = (frame + 1);
      if(frame >= 7) {
    	frame = 0;
      }
      timeout_state = setTimeout("animator()", 2200);
    }
    ...
    </head>
    <body>
    ...
    <div id="rotator">
      <img src="images/stress0.jpg" name="animimage" alt="stressed lady" height="168" width="125" /> <-- name is illegal
    </div>
    This doesn't work using what examples I can find.
    Code:
    function animator() {
      document.getElementById("animimage").innerHTML = anims[frame].src; <-- what do I use instead
      frame = (frame + 1);
      if(frame >= 7) {
        frame = 0;
      }
      timeout_state = setTimeout("animator()", 2200);
    }
    ...
    </head>
    <body>
    ...
    <div id="rotator">
      <img src="images/stress0.jpg" id="animimage" alt="stressed lady" height="168" width="125" /> <-- id is legal
    </div>
    I also tried
    Code:
    document.getElementById("animimage").value = anims[frame].src;
    to no avail

  2. #2
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,812
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    document.getElementById('animimage').src = anims[frame].src;
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">


Tags for this Thread

Bookmarks

Posting Permissions

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