1

How do I put the div id tag into an array an then output the image's alt text using a loop?

For e.g., I have the following code...

<div id="imageGallery" style="left: 800px; position: absolute; top: 200px">
	<center>
		<table border="0" cellspacing="0" cellpadding="3">
		<tr>
			<td><a href="#"><img alt="Random Image 1" src="../Images/thumbnails/random1.jpg"
			style="border-color: #000000; border-width: 2px; border-style: ridge"></a></td>
			<td><a href="#"><img alt="Random Image 2" src="../Images/thumbnails/random2.jpg"
			style="border-color: #000000; border-width: 2px; border-style: ridge"></a></td>
			<td><a href="#"><img alt="Random Image 3" src="../Images/thumbnails/buckstb01.jpg"
			style="border-color: #000000; border-width: 2px; border-style: ridge"></a></td>
		</tr>
		</table>
		<table border="0" cellspacing="0" cellpadding="3">
		<tr>
			<td><center><a href="#"><img alt="Random Image 4" src="../Images/thumbnails/portrait.jpg"
			style="border-color: #000000; border-width: 2px; border-style: ridge"></a></center></td>
		</tr>
		</table>
	</center>
</div>

I would like to put the id "imageGallery" into an array and output it's alt text afterwards.


To Caius

I've tried so many different variations.

var imageGallery = [ $( "#imageGallery img" ) ];

for( i = 0; i <= imageGallery.length; i++ )
{
     $( "#imageGallery" ).attr( "alt" );
}

Sometimes it would print out 1 letter at a time or just one set at a time, when I want the whole set.

I can't remember if I did it this way as I tried so many different variations and forgot to save it as it didn't work.

2
  • 2
    Post some code that shows what you've tried Commented Aug 1, 2016 at 16:39
  • Please quit trying to edit blex's answer into an updated question. Leave a comment if you need clarification on the answer, or edit your question if it needs updating. Commented Aug 3, 2016 at 18:42

3 Answers 3

1

// Here is the Array
var images = document.querySelectorAll('#imageGallery img');

for(var i = 0; i < images.length; i++){
  // Output the alt's in a loop
  console.log(images[i].getAttribute('alt'));
}
<div id="imageGallery" style="left: 800px; position: absolute; top: 200px">
	<center>
		<table border="0" cellspacing="0" cellpadding="3">
		<tr>
			<td><a href="#"><img alt="Random Image 1" src="../Images/thumbnails/random1.jpg"
			style="border-color: #000000; border-width: 2px; border-style: ridge"></a></td>
			<td><a href="#"><img alt="Random Image 2" src="../Images/thumbnails/random2.jpg"
			style="border-color: #000000; border-width: 2px; border-style: ridge"></a></td>
			<td><a href="#"><img alt="Random Image 3" src="../Images/thumbnails/buckstb01.jpg"
			style="border-color: #000000; border-width: 2px; border-style: ridge"></a></td>
		</tr>
		</table>
		<table border="0" cellspacing="0" cellpadding="3">
		<tr>
			<td><center><a href="#"><img alt="Random Image 4" src="../Images/thumbnails/portrait.jpg"
			style="border-color: #000000; border-width: 2px; border-style: ridge"></a></center></td>
		</tr>
		</table>
	</center>
</div>

Sign up to request clarification or add additional context in comments.

Comments

0

If you wish to output all the alt-text from elements with a specific html tag using jQuery, you could do so with the following code:

var $elements = $('img'); //replace 'img' with whichever tag you want to use
for (var i = 0; i < $elements.length; i++) {
  console.log($($elements[i]).attr('alt'));
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
<div id="imageGallery" style="left: 800px; position: absolute; top: 200px">
	<center>
		<table border="0" cellspacing="0" cellpadding="3">
		<tr>
			<td><a href="#"><img alt="Random Image 1" src="../Images/thumbnails/random1.jpg"
			style="border-color: #000000; border-width: 2px; border-style: ridge"></a></td>
			<td><a href="#"><img alt="Random Image 2" src="../Images/thumbnails/random2.jpg"
			style="border-color: #000000; border-width: 2px; border-style: ridge"></a></td>
			<td><a href="#"><img alt="Random Image 3" src="../Images/thumbnails/buckstb01.jpg"
			style="border-color: #000000; border-width: 2px; border-style: ridge"></a></td>
		</tr>
		</table>
		<table border="0" cellspacing="0" cellpadding="3">
		<tr>
			<td><center><a href="#"><img alt="Random Image 4" src="../Images/thumbnails/portrait.jpg"
			style="border-color: #000000; border-width: 2px; border-style: ridge"></a></center></td>
		</tr>
		</table>
	</center>
</div>

Comments

0

you can get the img tags into array

var arr = $($($("#imageGallery").find('td')).find('img'));

$.each(arr,function(i,data){

$(data).attr('alt')
})

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.