1

I have following html code.

<div id="polaroid">
    <figure>
        <img src="assets/polaroid01.jpg" width="200" height="200" alt="Red mushroom" />
        <figcaption>Pretty red mushroom</figcaption>
    </figure>
    <figure>
        <img src="assets/polaroid02.jpg" width="200" height="200" alt="Rainbow near Keswick" />
        <figcaption>Rainbow near Keswick</figcaption>
   </figure>
   <figure>
        <img src="assets/polaroid03.jpg" width="200" height="200" alt="An old tree" />
        <figcaption>Nice old tree</figcaption>
   </figure>
</div><!--end polaroid-->

In this i want to store all the image tags in an array. I know, I can access the figure tags like this.

var images= document.getElementById('gall').getElementsByTagName('figure'); 

But i don't know how to access the image tag. I tried this.

document.getElementById('gall').getElementsByTagName('figure').getElementsByTagName('img'); 

But this doesn't work.

1
  • it doesn't work because getElementsByTagName returns a node list, not a single node. Commented Sep 8, 2015 at 10:15

3 Answers 3

2

In this case it's more convenient to use querySelectorAll:

var images = document.querySelectorAll('#gall figure img');
Sign up to request clarification or add additional context in comments.

Comments

0

You mean like this:

var images= document.getElementById('gall').getElementsByTagName('figure');
images.getElementsByTagName("img");

1 Comment

hmm. What errors are you getting in the console when you try the above Sheldon?
0

This is possible if you use JQuery. Simply Do This.

  1. Include Jquery Library (any version).

  2. select image : $("#polaroid img").(any action);

Example:

put this above your code:

<script src="../js/jquery-1.7.2.min.js" type="text/javascript"></script>
<script type="text/javascript">
    
$(document).ready(function() { 

$("#polaroid img").css("border", "1px solid #000000");

});

</script>


Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.