Answer
Solution:
Reference the button that is clicked, select the parent, select the children
function imageLooper(btn) {
var images = btn.closest("div").getElementsByClassName("myImage");
for (var i = 0; i < images.length; i++) {
console.log(images[i]);
}
}
<div>
<img class="myImage" src="image1.png">
<img class="myImage" src="image2.png">
<img class="myImage" src="image3.png">
<button onclick="imageLooper(this)">Click here!</button>
</div>
<div>
<img class="myImage" src="image4.png">
<img class="myImage" src="image5.png">
<img class="myImage" src="image6.png">
<button onclick="imageLooper(this)">Click here!</button>
</div>
<div>
<img class="myImage" src="image7.png">
<img class="myImage" src="image8.png">
<img class="myImage" src="image9.png">
<button onclick="imageLooper(this)">Click here!</button>
</div>
<script src="js/main.js"></script>
Undefined answered
Link to answer
Answer
Solution:
Instead of:
var images = document.getElementsByClassName("myImage");
Add a parameter to your elements and pass itself in. For example:
<button onclick="imageLooper(this)">Click here!</button>
Note: 'this'.
Then define imageLooper as:
function imageLooper(me) {
var images = me.parentNode.getElementsByClassName("myImage");
}
Now, images contains only the images in the current block.
Undefined answered
Link to answer
Answer
Solution:
In this day and age using inline event handlers is not ideal - you can easily externalise this and use a single event listener usingquerySelectorAll
to identify nodes of interest.
document.querySelectorAll('div button').forEach( bttn=>{
bttn.addEventListener('click',e=>{
let col=e.target.parentNode.querySelectorAll('img');
col.forEach( img=>{
console.log( img )
})
})
})
<div>
<img class="myImage" src="image1.png">
<img class="myImage" src="image2.png">
<img class="myImage" src="image3.png">
<button>Click here!</button>
</div>
<div>
<img class="myImage" src="image4.png">
<img class="myImage" src="image5.png">
<img class="myImage" src="image6.png">
<button>Click here!</button>
</div>
<div>
<img class="myImage" src="image7.png">
<img class="myImage" src="image8.png">
<img class="myImage" src="image9.png">
<button>Click here!</button>
</div>
Undefined answered
Link to answer
Source
Share
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.
About the technologies asked in this question
JavaScript
JavaScript is a multi-paradigm language that supports event-driven, functional, and mandatory (including object-oriented and prototype-based) programming types. Originally JavaScript was only used on the client side. JavaScript is now still used as a server-side programming language. To summarize, we can say that JavaScript is the language of the Internet.
https://www.javascript.com/
HTML
HTML (English "hyper text markup language" - hypertext markup language) is a special markup language that is used to create sites on the Internet.
Browsers understand html perfectly and can interpret it in an understandable way. In general, any page on the site is html-code, which the browser translates into a user-friendly form. By the way, the code of any page is available to everyone.
https://www.w3.org/html/
Welcome to webdevask.com
Welcome to the Q&A site for web developers. Here you can ask a question about the problem you are facing and get answers from other experts. We have created a user-friendly interface so that you can quickly and free of charge ask a question about a web programming problem. We also invite other experts to join our community and help other members who ask questions. In addition, you can use our search for questions with a solution.
Get answers to specific questions
Ask about the real problem you are facing. Describe in detail what you are doing and what you want to achieve.
Help Others Solve Their Issues
Our goal is to create a strong community in which everyone will support each other. If you find a question and know the answer to it, help others with your knowledge.