document.getElementByI - dynamic use for using the same ID more than once

General Tech Learning Aids/Tools 2 years ago

0 2 0 0 0 tuteeHUB earn credit +10 pts

5 Star Rating 1 Rating

Posted on 16 Aug 2022, this text provides information on Learning Aids/Tools related to General Tech. Please note that while accuracy is prioritized, the data presented might not be entirely correct or up-to-date. This information is offered for general knowledge and informational purposes only, and should not be considered as a substitute for professional advice.

Take Quiz To Earn Credits!

Turn Your Knowledge into Earnings.

tuteehub_quiz

Answers (2)

Post Answer
profilepic.png
manpreet Tuteehub forum best answer Best Answer 2 years ago

 

var img = document.getElementById('myImg');
var modalImg = document.getElementById("img01");
var captionText = document.getElementById("caption");
img.onclick = function(){
    modal.style.display = "block";
    modalImg.src = this.src;
    modalImg.alt = this.alt;
    captionText.innerHTML = this.alt;
}
#myImg {
    border-radius: 5px;
    cursor: pointer;
    transition: 0.3s;
}
 id="myImg" src="imageone.jpg" alt="Image one" >



id="myImg" src="imagetwo.jpg" alt="Image two" >

So the above snippet is used in the modal image css3 tutorial. I understand i cannot have more than one tags with the same ID which is why the code online works with one image. After researching i know i have to use a dyamic ID with JS.

var img = document.getElementById('myImg');

The previous JS line is what i need to change.

How does one dynamically change getElementById? So that i can use more then one with differentr src images?

Research shows Using a Tag selector or a class selector is inappropriate.

Any help documents to aid in my learning will be great as well.


So i made some nice progress with the css and stuff and it looks great. Now my next step is to use existing alts to include text when the modal appears. Below is the existing js line i had already

var captionText = document.getElementById("caption");

Now below is the html i will use when calling alt so that each image has its own alt text appearing under the modal pop up

 

I've tried it and doesnt seem to work, could it be my formatting in the css?

profilepic.png
manpreet 2 years ago

f there's a collection of elements that need to be processed the same, it's best to use the classattribute to target them.

Instead of targeting one element with: document.getElementById, you get a NodeList, which is a collection of elements, using document.getElementsByClassName or document.querySelectorAll.

Then, you need to loop through these elements and use the code you've already written.

Here's an example:

var imgs = document.getElementsByClassName('myImg');

for (var i = 0; i < imgs.length; i += 1) {
  var img = imgs[i];

  img.onclick = function() {
    // your on click stuff
  };
};

Don't forget to change the HTML to: class="myImg" src="imageone.jpg" alt="Image one" >


0 views   0 shares

No matter what stage you're at in your education or career, TuteeHub will help you reach the next level that you're aiming for. Simply,Choose a subject/topic and get started in self-paced practice sessions to improve your knowledge and scores.