jdadwilson
05-24-2016, 02:09 AM
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...
<div id="myImage"></div>
Then in the js I have...
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...
<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
I use the following HTML to set the needed ID for the <a> tag...
<div id="myImage"></div>
Then in the js I have...
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...
<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