Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. JavaScript
  4. Need Help Setting <img> src attribute

Need Help Setting <img> src attribute

Scheduled Pinned Locked Moved JavaScript
javascripthtmldata-structurestestingbeta-testing
6 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    PianoMan
    wrote on last edited by
    #1

    I've hit a wall trying to use javascript to write a table with some image tags. I'm looping through an array of image names, then setting their src attribute. The function is creating the tags, but all that displays is the 'missing image' thumbnail. The function is fired via a button. Below is my code. Thanks!

    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <meta name="robots" content="noindex">

    <title>Testing Javascript</title>

    <script type="text/javascript">

    function getImages() {

    // Declare variables
    var allImages, vRow, vCell, vText, vImg;
    var vCnt, i, j, x, y;
    var vTable = document.createElement("TABLE");
    var vTBody = document.createElement("TBODY");

    // Insert vBody into vTable
    vTable.appendChild(vTBody);

    // create an array
    var images = new Array();

    images[0] = "image1.jpg";
    images[1] = "image2.jpg";
    images[2] = "image3.jpg";

    // count values in the array
    vCnt = images.length;

    // loop through the array of images and answers
    for (i=0; i < vCnt; i++)
    {
    // create a row
    vRow = document.createElement("TR");

    // create a TD tag
    vCell = document.createElement("TD");
    
    // create IMG element
    var image = images\[i\];
    vImg = document.createElement("IMG");
    vImg.setAttribute("src", "images/" + image);
    
    // insert the img tag into the td tag
    vCell.appendChild(vImg);
    
    // insert the cell into the row
    vRow.appendChild(vCell);
    
    // insert the row into the table
    vTBody.appendChild(vRow);
    

    }

    // add the table to the doc
    var vTableContainer = document.getElementById("vTableContainer");
    vTableContainer.appendChild(vTable);
    }
    // end updatePage()
    </script>
    </head>

    <body bgcolor = "#FFFFFF">

    Javascript and Images

    Trying to set the image source with js...

    <form method="GET">
    <input type="button" value="DISPLAY IMAGES" onClick="getImages();" />
    </form>

    </body>
    </html>

    D M T 3 Replies Last reply
    0
    • P PianoMan

      I've hit a wall trying to use javascript to write a table with some image tags. I'm looping through an array of image names, then setting their src attribute. The function is creating the tags, but all that displays is the 'missing image' thumbnail. The function is fired via a button. Below is my code. Thanks!

      <html>
      <head>
      <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
      <meta name="robots" content="noindex">

      <title>Testing Javascript</title>

      <script type="text/javascript">

      function getImages() {

      // Declare variables
      var allImages, vRow, vCell, vText, vImg;
      var vCnt, i, j, x, y;
      var vTable = document.createElement("TABLE");
      var vTBody = document.createElement("TBODY");

      // Insert vBody into vTable
      vTable.appendChild(vTBody);

      // create an array
      var images = new Array();

      images[0] = "image1.jpg";
      images[1] = "image2.jpg";
      images[2] = "image3.jpg";

      // count values in the array
      vCnt = images.length;

      // loop through the array of images and answers
      for (i=0; i < vCnt; i++)
      {
      // create a row
      vRow = document.createElement("TR");

      // create a TD tag
      vCell = document.createElement("TD");
      
      // create IMG element
      var image = images\[i\];
      vImg = document.createElement("IMG");
      vImg.setAttribute("src", "images/" + image);
      
      // insert the img tag into the td tag
      vCell.appendChild(vImg);
      
      // insert the cell into the row
      vRow.appendChild(vCell);
      
      // insert the row into the table
      vTBody.appendChild(vRow);
      

      }

      // add the table to the doc
      var vTableContainer = document.getElementById("vTableContainer");
      vTableContainer.appendChild(vTable);
      }
      // end updatePage()
      </script>
      </head>

      <body bgcolor = "#FFFFFF">

      Javascript and Images

      Trying to set the image source with js...

      <form method="GET">
      <input type="button" value="DISPLAY IMAGES" onClick="getImages();" />
      </form>

      </body>
      </html>

      D Offline
      D Offline
      Dean Oliver
      wrote on last edited by
      #2

      Hi you can either do it the standard way as it is shown in this article http://reference.sitepoint.com/javascript/Element/setAttribute[^] Or I suggest you rather use the jquery api. it's alot easier for manipulating tags etc; It'll make your life far easier. http://api.jquery.com/attr/[^] Here's an excellent beginner's guide. http://www.shopdev.co.uk/blog/jquery-from-scratch-a-beginners-guide/[^] Hope this all helps.

      P 1 Reply Last reply
      0
      • D Dean Oliver

        Hi you can either do it the standard way as it is shown in this article http://reference.sitepoint.com/javascript/Element/setAttribute[^] Or I suggest you rather use the jquery api. it's alot easier for manipulating tags etc; It'll make your life far easier. http://api.jquery.com/attr/[^] Here's an excellent beginner's guide. http://www.shopdev.co.uk/blog/jquery-from-scratch-a-beginners-guide/[^] Hope this all helps.

        P Offline
        P Offline
        PianoMan
        wrote on last edited by
        #3

        Dean, Thanks for the reply. I've been working with the jquery api just a bit today, and I really like it. Thanks for the reference!

        D 1 Reply Last reply
        0
        • P PianoMan

          I've hit a wall trying to use javascript to write a table with some image tags. I'm looping through an array of image names, then setting their src attribute. The function is creating the tags, but all that displays is the 'missing image' thumbnail. The function is fired via a button. Below is my code. Thanks!

          <html>
          <head>
          <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
          <meta name="robots" content="noindex">

          <title>Testing Javascript</title>

          <script type="text/javascript">

          function getImages() {

          // Declare variables
          var allImages, vRow, vCell, vText, vImg;
          var vCnt, i, j, x, y;
          var vTable = document.createElement("TABLE");
          var vTBody = document.createElement("TBODY");

          // Insert vBody into vTable
          vTable.appendChild(vTBody);

          // create an array
          var images = new Array();

          images[0] = "image1.jpg";
          images[1] = "image2.jpg";
          images[2] = "image3.jpg";

          // count values in the array
          vCnt = images.length;

          // loop through the array of images and answers
          for (i=0; i < vCnt; i++)
          {
          // create a row
          vRow = document.createElement("TR");

          // create a TD tag
          vCell = document.createElement("TD");
          
          // create IMG element
          var image = images\[i\];
          vImg = document.createElement("IMG");
          vImg.setAttribute("src", "images/" + image);
          
          // insert the img tag into the td tag
          vCell.appendChild(vImg);
          
          // insert the cell into the row
          vRow.appendChild(vCell);
          
          // insert the row into the table
          vTBody.appendChild(vRow);
          

          }

          // add the table to the doc
          var vTableContainer = document.getElementById("vTableContainer");
          vTableContainer.appendChild(vTable);
          }
          // end updatePage()
          </script>
          </head>

          <body bgcolor = "#FFFFFF">

          Javascript and Images

          Trying to set the image source with js...

          <form method="GET">
          <input type="button" value="DISPLAY IMAGES" onClick="getImages();" />
          </form>

          </body>
          </html>

          M Offline
          M Offline
          Mohibur Rashid
          wrote on last edited by
          #4

          I just checked about setAttribute. It seems that only Firefox implemented it properly. JQuery is a very good choice. for your information you can also use imgobject.src='my/image/source.jpg'; //it will do fine :)

          1 Reply Last reply
          0
          • P PianoMan

            Dean, Thanks for the reply. I've been working with the jquery api just a bit today, and I really like it. Thanks for the reference!

            D Offline
            D Offline
            Dean Oliver
            wrote on last edited by
            #5

            Please vote if you found my answer useful. thanks :)

            1 Reply Last reply
            0
            • P PianoMan

              I've hit a wall trying to use javascript to write a table with some image tags. I'm looping through an array of image names, then setting their src attribute. The function is creating the tags, but all that displays is the 'missing image' thumbnail. The function is fired via a button. Below is my code. Thanks!

              <html>
              <head>
              <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
              <meta name="robots" content="noindex">

              <title>Testing Javascript</title>

              <script type="text/javascript">

              function getImages() {

              // Declare variables
              var allImages, vRow, vCell, vText, vImg;
              var vCnt, i, j, x, y;
              var vTable = document.createElement("TABLE");
              var vTBody = document.createElement("TBODY");

              // Insert vBody into vTable
              vTable.appendChild(vTBody);

              // create an array
              var images = new Array();

              images[0] = "image1.jpg";
              images[1] = "image2.jpg";
              images[2] = "image3.jpg";

              // count values in the array
              vCnt = images.length;

              // loop through the array of images and answers
              for (i=0; i < vCnt; i++)
              {
              // create a row
              vRow = document.createElement("TR");

              // create a TD tag
              vCell = document.createElement("TD");
              
              // create IMG element
              var image = images\[i\];
              vImg = document.createElement("IMG");
              vImg.setAttribute("src", "images/" + image);
              
              // insert the img tag into the td tag
              vCell.appendChild(vImg);
              
              // insert the cell into the row
              vRow.appendChild(vCell);
              
              // insert the row into the table
              vTBody.appendChild(vRow);
              

              }

              // add the table to the doc
              var vTableContainer = document.getElementById("vTableContainer");
              vTableContainer.appendChild(vTable);
              }
              // end updatePage()
              </script>
              </head>

              <body bgcolor = "#FFFFFF">

              Javascript and Images

              Trying to set the image source with js...

              <form method="GET">
              <input type="button" value="DISPLAY IMAGES" onClick="getImages();" />
              </form>

              </body>
              </html>

              T Offline
              T Offline
              twseitex
              wrote on last edited by
              #6

              (1) Please don't use names of collections for private vars so var images = new Array(); better var arArrayOfImagesObjects=new Array(); images is al pre-dfined collection: document.images see http://msdn.microsoft.com/en-us/library/ms537461.aspx (2) To set scr: valueof .src is not same like pure string preload image var imgObjekt1=new Image(); // height and width if you want imgObjekt1.src='whatYouWant.jpg'; // maybe with path vImg = document.createElement("IMG"); // check pionter ! if(vImg!=null) { // append into object (into child of body) // append into collection images automatically // check pointer of appended object, use e.g. var imgAppendedImgObject if(imgAppendedImgObject!=null) {imgAppendedImgObject.src=imgObjekt1.src; // will be (now) rendered in body // don't use setAttribute("src", "images/" + image); } } (3) FIRST optimize your code during edit and THEN check errors at run time. to createElement and appendChild check always pointers != null if you use a collection like images check too declare vars inside of function --> local vars declare vars inside of for{} --> declare 1 time outside of for and use inside declare constants like "image1.jpg" outside of function --> use global code e.g. at begin of js-file (easy find and edit in js-file).

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups