Results 1 to 3 of 3

Thread: Dynamically build multiple <a><img></a> elements

  1. #1
    Join Date
    Feb 2013
    Location
    California
    Posts
    86
    Thanks
    12
    Thanked 0 Times in 0 Posts

    Default Dynamically build multiple <a><img></a> elements

    I have need to dynamically build via JavaScript multiple <a><img></a> type elements. This is simple for single elements given that the <a> tag can be given an ID for the href as can the <img> tag for the src. BUT, how does one use JavaScript to do this?

    I use the following HTML to set the needed ID for the <a> tag...

    HTML Code:
    <div id="myImage"></div>
    Then in the js I have...

    Code:
    imagePath = 'files_images/myImage.jpg';
    myElement = document.createElement('a');
    myElement.setAttribute('href', imagePath);
    myElement.setAttribute('target', '_blank');
    myElement.setAttribute('style', 'float:right; display:inline; margin:0;');
    myElement.setAttribute('border', 0);
    document.getElementById('myImage').appendChild(myElement);
    to set the <a> tag.

    But I also need to include the <img> tag as the text of the <a> tag such as...

    HTML Code:
    <a href="files_images/myImage.jpg"><img src="files_images/myImage.jpg"></a>
    The <a> tag cannot be created with an ID as there are an unknown number of images that can be created per page.

    TIA for your assistance
    jdadwilson

  2. #2
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    Where are you getting all the images from? Do you have an array of paths to them? If so you can iterate over the array to create each link and image*. If the images are provided in some other fashion, it can probably be worked with. But we would need to know. Also, are all of these links with their images to go in the myImage div? This we also need to know, and if not, where do they go?

    Your question also includes asking how to get the image tag into the link tag. That part is easy if we are to work within the given structure:

    Code:
    imagePath = 'files_images/myImage.jpg';
    myElement = document.createElement('a');
    myElement.setAttribute('href', imagePath);
    myElement.setAttribute('target', '_blank');
    myElement.setAttribute('style', 'float:right; display:inline; margin:0;');
    myElement.setAttribute('border', 0);
    anImage = document.createElement('img');
    anImage.setAttribute('src', imagePath);
    myElement.appendChild(anImage);
    document.getElementById('myImage').appendChild(myElement);

    *For example, given your existing div, this code:

    Code:
    var myImages = [
    	'files_images/myImage1.jpg',
    	'files_images/myImage2.jpg',
    	'files_images/myImage3.jpg'
    ], lim = myImages.length, imagePath, myElement, anImage, i = -1;
    
    while (++i < lim){
    	imagePath = myImages[i];
    	myElement = document.createElement('a');
    	myElement.setAttribute('href', imagePath);
    	myElement.setAttribute('target', '_blank');
    	myElement.setAttribute('style', 'float:right; display:inline; margin:0;');
    	myElement.setAttribute('border', 0);
    	anImage = document.createElement('img');
    	anImage.setAttribute('src', imagePath);
    	myElement.appendChild(anImage);
    	document.getElementById('myImage').appendChild(myElement);
    }
    Would produce this virtual markup:

    HTML Code:
    <div id="myImage"><a href="files_images/myImage1.jpg" target="_blank" style="float:right; display:inline; margin:0;" border="0"><img src="files_images/myImage1.jpg"></a><a href="files_images/myImage2.jpg" target="_blank" style="float:right; display:inline; margin:0;" border="0"><img src="files_images/myImage2.jpg"></a><a href="files_images/myImage3.jpg" target="_blank" style="float:right; display:inline; margin:0;" border="0"><img src="files_images/myImage3.jpg"></a></div>
    Last edited by jscheuer1; 05-24-2016 at 03:08 AM. Reason: add further example
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  3. #3
    Join Date
    Feb 2013
    Location
    California
    Posts
    86
    Thanks
    12
    Thanked 0 Times in 0 Posts

    Default

    Thanks once again... Your example exactly answered my question. Being a novice with JavaScript I was thinking that myElement would have to be appended before anImage.

    jdadwilson

Similar Threads

  1. Replies: 4
    Last Post: 02-25-2014, 06:11 PM
  2. Replies: 1
    Last Post: 05-22-2013, 04:28 AM
  3. Dynamically add form elements
    By Stom in forum JavaScript
    Replies: 5
    Last Post: 06-07-2008, 07:21 AM
  4. Replies: 7
    Last Post: 11-09-2007, 12:49 AM
  5. Replies: 0
    Last Post: 02-20-2005, 09:51 AM

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
  •