Modal images have troubles with classes - javascript

I got this modal script to display bigger images on my site. It worked well, but only with 1 image. When i tried to add more images - script didn't worked, and nothing was happening. Can someone tell me what am I doing wrong?
Here's some code:
var modal = document.getElementById("myModal");
// Get the image and insert it inside the modal - use its "alt" text as a caption
var img = document.getElementsByClassName("zdjecie");
var modalImg = document.getElementById("img01");
var captionText = document.getElementById("caption");
img.onclick = function() {
modal.style.display = "block";
modalImg.src = this.src;
captionText.innerHTML = this.alt;
}
// Get the <span> element that closes the modal
var span = document.getElementsByClassName("close")[0];
// When the user clicks on <span> (x), close the modal
span.onclick = function() {
modal.style.display = "none";
}
<img class="zdjecie" style="max-height: 16rem;max-width: 16rem;" src="example.jpg">
When I was using ID's instead of classes it worked well - but i couldn't add more images - that's why i need to use classes. Someone has any ideas? I'd be greatful

The document.getElementsByClassName returns an array, which contains all the dom has the zdjecie class.
So you have to loop through this array to add the event, or put these images inside a container and use event-delegation, which is a more efficient way. Because it just bind event once. More about event-delegation: https://javascript.info/event-delegation`
Here is the code using event-delegation.
var handleImageClick = function(evt){
if(evt.target.className.indexOf('zdjecie') === -1) return;
modal.style.display = "block";
modalImg.src = evt.target.src;
captionText.innerHTML = evt.target.alt;
}
// Get the image and insert it inside the modal - use its "alt" text as a caption
var modal = document.getElementById("myModal");
var imageContainer = document.getElementById("imageContainer");
var modalImg = document.getElementById("img01");
var captionText = document.getElementById("caption");
imageContainer.addEventListener('click', handleImageClick)
// Get the <span> element that closes the modal
var span = document.getElementsByClassName("close")[0];
// When the user clicks on <span> (x), close the modal
span.onclick = function() {
modal.style.display = "none";
}
.zdjecie{
background-color: yellow;
width: 100px;
height: 100px;
display: inline-block;
}
<div id="imageContainer">
<img class="zdjecie" alt="1" style="max-height: 16rem;max-width: 16rem;" src="example1.jpg" />
<img class="zdjecie" alt="2" style="max-height: 16rem;max-width: 16rem;" src="example2.jpg" />
<img class="zdjecie" alt="3" style="max-height: 16rem;max-width: 16rem;" src="example3.jpg" />
<img class="zdjecie" alt="4" style="max-height: 16rem;max-width: 16rem;" src="example4.jpg" />
</div>
<div id="myModal">
<span class="close">x</span>
<img id="img01" style="max-height: 16rem;max-width: 16rem;" />
<p id="caption"></p>
</div>

Related

Modal Image Issue in JavaScript

I'm trying to include an image modal into my photo gallery. I've been trying to work with a version of this example:
http://www-db.deis.unibo.it/courses/TW/DOCS/w3schools/howto/howto_css_modal_images.asp.html
The example only works with one image since it accesses the image through its ID. I've tried to modify it and just have it access the image through its class. But that doesn't work. How would I have to modify the JS code to make it work with any image in a gallery?
Thank you!!!
Modified code:
HTML:
<img class="galleryPic" src="assets/images/test images/globes.jpg" alt="Old globes" width="300" height="200">
<img class="galleryPic" src="assets/images/test images/palmtrees.jpg" alt="Palmtrees in front of the evening sky" width="300" height="200">
<!-- The Modal -->
<div id="myModal" class="modal">
<!-- The Close Button -->
<span class="close" onclick="document.getElementById('myModal').style.display='none'">×</span>
<!-- Modal Content (The Image) -->
<img class="modal-content" id="img01">
<!-- Modal Caption (Image Text) -->
<div id="caption"></div>
</div>
JS:
// Get the modal
var modal = document.getElementById('myModal');
// Get the image and insert it inside the modal - use its "alt" text as a caption
var img = document.getElementsByClass('galleryPic');
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;
}
// Get the <span> element that closes the modal
var span = document.getElementsByClassName("close")[0];
// When the user clicks on <span> (x), close the modal
span.onclick = function() {
modal.style.display = "none";
}
There is not such thing as document.getElementsByClass(), there is document.getElementsByClassName() which returns a list of elements if found.
So you'll need "loop" through the list:
// Get the modal
var modal = document.getElementById('myModal');
// Get the image and insert it inside the modal - use its "alt" text as a caption
var imgs = document.getElementsByClassName('galleryPic');
var modalImg = document.getElementById("img01");
var captionText = document.getElementById("caption");
for (let i = 0; i < imgs.length; i++) {
imgs[i].onclick = function() {
modal.style.display = "block";
modalImg.src = this.src;
modalImg.alt = this.alt;
captionText.innerHTML = this.alt;
}
}
// Get the <span> element that closes the modal
var span = document.getElementsByClassName("close")[0];
// When the user clicks on <span> (x), close the modal
span.onclick = function() {
modal.style.display = "none";
}
<img class="galleryPic" src="https://lh3.googleusercontent.com/9pPCK70Rw0k3wethMHb1qMaIB0VjeWLy57vYgSzKbF7oJuvO2nA0Nakk-95cvibWUDcEhYkfCKvdPKT03tXZd4M5jdhIEibLO9qw-XE=w1024-h683-n-l50-sg-rj" alt="Old globes" width="300" height="200">
<img class="galleryPic" src="https://lh3.googleusercontent.com/taykG37GWDgY-FGkdogDvsHSJMUGRMvkuVRT6yR-5UNkKvGRKeRlpGYXlslocOcS0txlfUdGW59JGtzADknxbMqnh6AtVCv9EXyB8nHp80YsRNA0Yw=w1024-h683-n-l50-sg-rj" alt="Palmtrees in front of the evening sky" width="300" height="200">
<!-- The Modal -->
<div id="myModal" class="modal">
<!-- The Close Button -->
<span class="close" onclick="document.getElementById('myModal').style.display='none'">×</span>
<!-- Modal Content (The Image) -->
<img class="modal-content" id="img01">
<!-- Modal Caption (Image Text) -->
<div id="caption"></div>
</div>

How to show future images actual size when click

When I run the code only the first image is showing on click and when I click others, the first image will be the one to show.
var modal = document.getElementById("myModal");
function my(course) {
var img = document.querySelectorAll('#myDiv img')[0];
var modalImg = document.getElementById("img01");
var captionText = document.getElementById("caption");
modal.style.display = "block";
modalImg.src = '<?php echo $image[0] ?>';
captionText.innerHTML = "future Ad full image";
var span = document.getElementsByClassName("close")[0];
// When the user clicks on <span> (x), close the modal
span.onclick = function() {
modal.style.display = "none";
}
}
<div class="item <?php if($i == 0 ) echo 'active'; ?>" id="myDiv"><img onClick="my(this);" class="myImg" id="my" style="height:400px; object-fit: cover;" src="<?php echo $image[0] ?>"></div>
<div id="myModal" class="modal">
<span class="close">×</span>
<img class="modal-content" id="img01">
<div id="caption"></div>
</div>
</div>
When i run the code only the first image is showing on click and when i clik others the first image will be the one to show. have try my best please i need help

Adding javascript buttons to a Modal (javascript newbie)

I'm new to java-script and really programming, I'm able to read and understand whats going on but writing code is another issue. It drives me crazy I can't figure out some buttons. Does anyone have any recommendations on how to get these modals some buttons?
<div>
<!-- Trigger the Modal -->
<a img id="myImg30" src="homefiles/a (30).jpg" alt="Snow" style="width:100%;max-width:300px"></a>
<!-- The Modal -->
<div id="myModal" class="modal">
<!-- The Close Button -->
<span class="close">×</span>
<!-- Modal Content (The Image) -->
<img class="modal-content" id="img01">
<!-- Modal Caption (Image Text) -->
<div id="caption"></div>
</div>
</div>
<script>
// Get the modal
var modal = document.getElementById("myModal");
// Get the image and insert it inside the modal - use its "alt" text as a caption
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;
captionText.innerHTML = this.alt;
}
// Get the <span> element that closes the modal
var span = document.getElementsByClassName("close")[0];
// When the user clicks on <span> (x), close the modal
span.onclick = function () {
}
</script>

Dynamic image in modal view with javascript in Laravel 5.2

I need a little advice to display images dynamically.
I am currently using this script from an example of W3Schools:
http://www.w3schools.com/howto/howto_css_modal_images.asp
Here is the script:
<script>
// Get the modal
var modal = document.getElementById('myModal');
// Get the image and insert it inside the modal - use its "alt" text as a caption
var img = document.getElementById('myImg');
var modalImg = document.getElementById("img");
var captionText = document.getElementById("caption");
img.onclick = function(){
modal.style.display = "block";
modalImg.src = this.src;
captionText.innerHTML = this.alt;
}
// Get the <span> element that closes the modal
var span = document.getElementsByClassName("close")[0];
// When the user clicks on <span> (x), close the modal
span.onclick = function() {
modal.style.display = "none";
}
</script>
#foreach($properties->files as $file)
<div class="col-md-6 thumb">
<a class="thumbnail">
<img id="myImg" src="{{ URL::asset('uploads/products/' . $file->name) }}" alt="{{ $file->name }}" width="300" height="200">
</a>
</div>
<div id="myModal" class="modal">
<span class="close" onclick="document.getElementById('myModal').style.display='none'">×</span>
<img class="modal-content" id="img">
<div id="caption"></div>
</div>
#endforeach
I need to display the images that are displayed dynamically in the thumbnail, because only the first image in the list is showing.
In the view the images are shown like this:
The first image is displayed in a modal view, but the second does not:
This is my show method in Propertycontroller:
Public function show ($ id)
     {
         $ Properties = Property :: find ($ id);
        
         $ Files = File :: where ('property_id', $ properties-> id) -> get ();
        
         Return view ('properties.show', compact ('properties', 'files'));
     }
This requirement is related to:
Show image file by id in Laravel 5.2
How can I display the images in the modal view when clicking them?
It's showing only first image because your id will be same for all images. Use for loop index to give unique id to img tag like below :
#foreach($properties->files as $index=>$file)
<div class="col-md-6 thumb">
<a class="thumbnail">
<img id="myImg<?php echo $index ?>" src="{{ URL::asset('uploads/products/' . $file->name) }}" alt="{{ $file->name }}" width="300" height="200" onclick="showImage(this,<?php echo $index ?>)">
</a>
</div>
#endforeach
showImage() function will pass the unique $index to your script and do the thing.
<script>
function showImage(element,i){
// Get the modal
var modal = document.getElementById('myModal');
// Get the image and insert it inside the modal - use its "alt" text as a caption
var img = document.getElementById('myImg'+i);
var modalImg = document.getElementById("img");
var captionText = document.getElementById("caption");
modal.style.display = "block";
modalImg.src = element.src;
captionText.innerHTML = element.alt;
}
</script>
similarly you can set closing the modal logic. I hope this help you.

Trouble adding multiple images to CSS/JS modal

I am trying to make a few images pop up individually in a modal. I can only seem to get the first image to pop up, but not the 2nd. I would prefer to make the images pop up in a modal that will let you switch between them, but I at least need them to pop up individually.
Html:
<div class="container">
<img id="myImg" src="images/300-200.png" alt="Filler Text" width="300" height="200">
<img id="myImg2" src="images/300-200.png" alt="Filler Text" width="300" height="200">
<div id="myModal" class="modal">
<span class="closeimg" onclick="document.getElementById('myModal').style.display='none'">×</span>
<img class="modal-content" id="img01">
<div id="caption"></div>
</div>
</div>
Javascript:
var modal = document.getElementById('myModal');
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;
captionText.innerHTML = this.alt;
}
var span = document.getElementsByClassName("close")[0];
span.onclick = function() {
modal.style.display = "none";
}
I know there is an issue since I can't use .getElementByID to pick up more than one ID and I know that two elements can't be labeled the same ID. With that being said, I have tried .getElementByClassName which didn't work either.
When trying to select multiple elements of the same kind you should use a class selector since it has the ability to appear multiple times on a page, where an id is unique to one element.
So, first we'll add class="myImg" to each of the two images.
<img id="myImg" class="myImg" src="images/300-200.png" alt="Filler Text" width="300" height="200">
<img id="myImg2" class="myImg" src="images/300-200.png" alt="Filler Text" width="300" height="200">
Then we'll change how we're selecting the images:
// From this
var img = document.getElementById('myImg');
// To this
var imgs = document.getElementsByClassName('myImg');
Since getElementsByClassName returns the array-like HTMLCollection type, you need to iterate through each element, then add the event listener to it.
for (i = 0; i < imgs.length; i = i + 1) {
var img = imgs[i];
img.onclick = function(){
modal.style.display = "block";
modalImg.src = this.src;
captionText.innerHTML = this.alt;
};
};
Here's a working version of this example:
var modal = document.getElementById('myModal');
var imgs = document.getElementsByClassName('myImg');
var modalImg = document.getElementById("img01");
var captionText = document.getElementById("caption");
for (i = 0; i < imgs.length; i = i + 1) {
var img = imgs[i];
img.onclick = function(){
modal.style.display = "block";
modalImg.src = this.src;
captionText.innerHTML = this.alt;
};
};
var span = document.getElementsByClassName("closeimg")[0];
span.onclick = function() {
modal.style.display = "none";
};
.modal {
display: none;
padding: 10px;
background-color: blue;
}
<div class="container">
<img id="myImg" class="myImg" src="http://placekitten.com/300/200" alt="Cat 1" width="300" height="200">
<img id="myImg2" class="myImg" src="http://placekitten.com/300/300" alt="Cat 1" width="300" height="200">
<div id="myModal" class="modal">
<span class="closeimg">×</span>
<img class="modal-content" id="img01">
<div id="caption"></div>
</div>
</div>
<div class="container">
<img class="img" src="images/300-200.png" alt="Filler Text" width="300" height="200">
<img class="img" src="images/300-200.png" alt="Filler Text" width="300" height="200">
<div id="myModal" class="modal">
<span class="closeimg" onclick="document.getElementById('myModal').style.display='none'">×</span>
<img id="modal-img">
<div id="caption"></div>
</div>
</div>
var img = document.querySelectorAll('img'),
modal = document.getElementById('myModal'),
modal_img = document.getElementById('modal-img'),
captionText = document.getElementById('caption');
[].forEach.call(img, function(element){
element.onclick = function(){
modal.style.display = 'block';
modal_img.src = this.getAttribute('src');
caption.innerHTML = this.getAttribute('alt');
}
});
Working Fiddle: https://jsfiddle.net/kmdqjmua/
Try it.

Categories

Resources