Image insert inside the modal popup
I'm making a Image Gallery with model popup, The JavaScript code is working, but only with first image of the gallery popup modal box. How can i use every image popup modal ?
Any Help is Greatly Appreciated, thanks.
<!-- The Modal -->
<div id="myModal" class="modal">
<span class="close">×</span>
<img class="modal-content" id="myImg1">
<div id="caption"></div>
</div>
<?php echo '<img id="myImg" src="'.esc_url( $info['0'] ).'" alt="Snow" style="width:100%;max-width:300px">'; ?>
<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("myImg1");
img.onclick = function(){
modal.style.display = "block";
modalImg.src = this.src;
}
// 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>
javascript
add a comment |
I'm making a Image Gallery with model popup, The JavaScript code is working, but only with first image of the gallery popup modal box. How can i use every image popup modal ?
Any Help is Greatly Appreciated, thanks.
<!-- The Modal -->
<div id="myModal" class="modal">
<span class="close">×</span>
<img class="modal-content" id="myImg1">
<div id="caption"></div>
</div>
<?php echo '<img id="myImg" src="'.esc_url( $info['0'] ).'" alt="Snow" style="width:100%;max-width:300px">'; ?>
<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("myImg1");
img.onclick = function(){
modal.style.display = "block";
modalImg.src = this.src;
}
// 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>
javascript
I'm assuming all your <img> tags have the idmyImg
, which is a conflict as many different elements are using the same id andgetElementById
is only attached to the first occurence. Learn More
– Jaswinder Singh
Nov 13 at 11:48
add a comment |
I'm making a Image Gallery with model popup, The JavaScript code is working, but only with first image of the gallery popup modal box. How can i use every image popup modal ?
Any Help is Greatly Appreciated, thanks.
<!-- The Modal -->
<div id="myModal" class="modal">
<span class="close">×</span>
<img class="modal-content" id="myImg1">
<div id="caption"></div>
</div>
<?php echo '<img id="myImg" src="'.esc_url( $info['0'] ).'" alt="Snow" style="width:100%;max-width:300px">'; ?>
<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("myImg1");
img.onclick = function(){
modal.style.display = "block";
modalImg.src = this.src;
}
// 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>
javascript
I'm making a Image Gallery with model popup, The JavaScript code is working, but only with first image of the gallery popup modal box. How can i use every image popup modal ?
Any Help is Greatly Appreciated, thanks.
<!-- The Modal -->
<div id="myModal" class="modal">
<span class="close">×</span>
<img class="modal-content" id="myImg1">
<div id="caption"></div>
</div>
<?php echo '<img id="myImg" src="'.esc_url( $info['0'] ).'" alt="Snow" style="width:100%;max-width:300px">'; ?>
<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("myImg1");
img.onclick = function(){
modal.style.display = "block";
modalImg.src = this.src;
}
// 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>
javascript
javascript
edited Nov 13 at 13:17
Jaswinder Singh
291210
291210
asked Nov 13 at 11:31
noufal
144
144
I'm assuming all your <img> tags have the idmyImg
, which is a conflict as many different elements are using the same id andgetElementById
is only attached to the first occurence. Learn More
– Jaswinder Singh
Nov 13 at 11:48
add a comment |
I'm assuming all your <img> tags have the idmyImg
, which is a conflict as many different elements are using the same id andgetElementById
is only attached to the first occurence. Learn More
– Jaswinder Singh
Nov 13 at 11:48
I'm assuming all your <img> tags have the id
myImg
, which is a conflict as many different elements are using the same id and getElementById
is only attached to the first occurence. Learn More– Jaswinder Singh
Nov 13 at 11:48
I'm assuming all your <img> tags have the id
myImg
, which is a conflict as many different elements are using the same id and getElementById
is only attached to the first occurence. Learn More– Jaswinder Singh
Nov 13 at 11:48
add a comment |
3 Answers
3
active
oldest
votes
Please try this. Add this line for get defined src
var myImg= document.getElementById("myImg");
Replace Your JavaScript.
function imgPopup(){
var modal = document.getElementById('myModal');
var modalImg = document.getElementById("myImg1");
var myImg= document.getElementById("myImg");
modal.style.display = "block";
modalImg.src = myImg.src;
var span = document.getElementsByClassName("close")[0];
span.onclick = function() {
modal.style.display = "none";
}
}
add a comment |
The problem could be that all your images have same id myImg.
var img = document.getElementById('myImg');
always select the first element with given id. You could try putting the js in a function and calling it onclick.
function imgPopup(){
var modal = document.getElementById('myModal');
var modalImg = document.getElementById("myImg1");
modal.style.display = "block";
modalImg.src = this.src;
}
<?php echo '<img id="myImg" src="'.esc_url( $info['0'] ).'" onclick="imgPopup();" alt="Snow" style="width:100%;max-width:300px">'; ?>
SRC= Undefined what is the problem.
– noufal
Nov 14 at 4:51
browser showing src not defined( myimg1) What I do? and How i can impliment close button?
– noufal
Nov 14 at 5:14
add a comment |
Do the following updates: wrap you new images in a container. Add the event listener to the container, match the tag you want(img) then update the src element in the modal and show it:
<!-- The Modal -->
<div id="myModal" class="modal">
<span class="close">×</span>
<img class="modal-content" id="myImg1">
<div id="caption"></div>
</div>
<div id="gallery">
<?php echo '<img src="'.esc_url( $info['0'] ).'" alt="Snow" style="width:100%;max-width:300px">'; ?>
</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 gallery = document.getElementById('gallery'),
modalImg = document.getElementById("myImg1");
gallery.addEventListener('click', function(e){
if(e.target.nodeName.toLowerCase() === 'img'){
modalImg.src = e.target.src;
modal.style.display = "block";
}
});
// 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>
You can also refactor a lot this piece of code... set an ID to modal, then only hook by tags, set the close event on the container and so on...
– n1kkou
Nov 13 at 13:31
Nothing happen on click.
– noufal
Nov 14 at 5:10
Yes, my bad, I updated the answer. I used "p" tag instead of "img"
– n1kkou
Nov 14 at 7:29
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53280096%2fimage-insert-inside-the-modal-popup%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
Please try this. Add this line for get defined src
var myImg= document.getElementById("myImg");
Replace Your JavaScript.
function imgPopup(){
var modal = document.getElementById('myModal');
var modalImg = document.getElementById("myImg1");
var myImg= document.getElementById("myImg");
modal.style.display = "block";
modalImg.src = myImg.src;
var span = document.getElementsByClassName("close")[0];
span.onclick = function() {
modal.style.display = "none";
}
}
add a comment |
Please try this. Add this line for get defined src
var myImg= document.getElementById("myImg");
Replace Your JavaScript.
function imgPopup(){
var modal = document.getElementById('myModal');
var modalImg = document.getElementById("myImg1");
var myImg= document.getElementById("myImg");
modal.style.display = "block";
modalImg.src = myImg.src;
var span = document.getElementsByClassName("close")[0];
span.onclick = function() {
modal.style.display = "none";
}
}
add a comment |
Please try this. Add this line for get defined src
var myImg= document.getElementById("myImg");
Replace Your JavaScript.
function imgPopup(){
var modal = document.getElementById('myModal');
var modalImg = document.getElementById("myImg1");
var myImg= document.getElementById("myImg");
modal.style.display = "block";
modalImg.src = myImg.src;
var span = document.getElementsByClassName("close")[0];
span.onclick = function() {
modal.style.display = "none";
}
}
Please try this. Add this line for get defined src
var myImg= document.getElementById("myImg");
Replace Your JavaScript.
function imgPopup(){
var modal = document.getElementById('myModal');
var modalImg = document.getElementById("myImg1");
var myImg= document.getElementById("myImg");
modal.style.display = "block";
modalImg.src = myImg.src;
var span = document.getElementsByClassName("close")[0];
span.onclick = function() {
modal.style.display = "none";
}
}
answered Nov 14 at 6:02
Noufal Binu
9010
9010
add a comment |
add a comment |
The problem could be that all your images have same id myImg.
var img = document.getElementById('myImg');
always select the first element with given id. You could try putting the js in a function and calling it onclick.
function imgPopup(){
var modal = document.getElementById('myModal');
var modalImg = document.getElementById("myImg1");
modal.style.display = "block";
modalImg.src = this.src;
}
<?php echo '<img id="myImg" src="'.esc_url( $info['0'] ).'" onclick="imgPopup();" alt="Snow" style="width:100%;max-width:300px">'; ?>
SRC= Undefined what is the problem.
– noufal
Nov 14 at 4:51
browser showing src not defined( myimg1) What I do? and How i can impliment close button?
– noufal
Nov 14 at 5:14
add a comment |
The problem could be that all your images have same id myImg.
var img = document.getElementById('myImg');
always select the first element with given id. You could try putting the js in a function and calling it onclick.
function imgPopup(){
var modal = document.getElementById('myModal');
var modalImg = document.getElementById("myImg1");
modal.style.display = "block";
modalImg.src = this.src;
}
<?php echo '<img id="myImg" src="'.esc_url( $info['0'] ).'" onclick="imgPopup();" alt="Snow" style="width:100%;max-width:300px">'; ?>
SRC= Undefined what is the problem.
– noufal
Nov 14 at 4:51
browser showing src not defined( myimg1) What I do? and How i can impliment close button?
– noufal
Nov 14 at 5:14
add a comment |
The problem could be that all your images have same id myImg.
var img = document.getElementById('myImg');
always select the first element with given id. You could try putting the js in a function and calling it onclick.
function imgPopup(){
var modal = document.getElementById('myModal');
var modalImg = document.getElementById("myImg1");
modal.style.display = "block";
modalImg.src = this.src;
}
<?php echo '<img id="myImg" src="'.esc_url( $info['0'] ).'" onclick="imgPopup();" alt="Snow" style="width:100%;max-width:300px">'; ?>
The problem could be that all your images have same id myImg.
var img = document.getElementById('myImg');
always select the first element with given id. You could try putting the js in a function and calling it onclick.
function imgPopup(){
var modal = document.getElementById('myModal');
var modalImg = document.getElementById("myImg1");
modal.style.display = "block";
modalImg.src = this.src;
}
<?php echo '<img id="myImg" src="'.esc_url( $info['0'] ).'" onclick="imgPopup();" alt="Snow" style="width:100%;max-width:300px">'; ?>
answered Nov 13 at 12:28
Atul
407
407
SRC= Undefined what is the problem.
– noufal
Nov 14 at 4:51
browser showing src not defined( myimg1) What I do? and How i can impliment close button?
– noufal
Nov 14 at 5:14
add a comment |
SRC= Undefined what is the problem.
– noufal
Nov 14 at 4:51
browser showing src not defined( myimg1) What I do? and How i can impliment close button?
– noufal
Nov 14 at 5:14
SRC= Undefined what is the problem.
– noufal
Nov 14 at 4:51
SRC= Undefined what is the problem.
– noufal
Nov 14 at 4:51
browser showing src not defined( myimg1) What I do? and How i can impliment close button?
– noufal
Nov 14 at 5:14
browser showing src not defined( myimg1) What I do? and How i can impliment close button?
– noufal
Nov 14 at 5:14
add a comment |
Do the following updates: wrap you new images in a container. Add the event listener to the container, match the tag you want(img) then update the src element in the modal and show it:
<!-- The Modal -->
<div id="myModal" class="modal">
<span class="close">×</span>
<img class="modal-content" id="myImg1">
<div id="caption"></div>
</div>
<div id="gallery">
<?php echo '<img src="'.esc_url( $info['0'] ).'" alt="Snow" style="width:100%;max-width:300px">'; ?>
</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 gallery = document.getElementById('gallery'),
modalImg = document.getElementById("myImg1");
gallery.addEventListener('click', function(e){
if(e.target.nodeName.toLowerCase() === 'img'){
modalImg.src = e.target.src;
modal.style.display = "block";
}
});
// 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>
You can also refactor a lot this piece of code... set an ID to modal, then only hook by tags, set the close event on the container and so on...
– n1kkou
Nov 13 at 13:31
Nothing happen on click.
– noufal
Nov 14 at 5:10
Yes, my bad, I updated the answer. I used "p" tag instead of "img"
– n1kkou
Nov 14 at 7:29
add a comment |
Do the following updates: wrap you new images in a container. Add the event listener to the container, match the tag you want(img) then update the src element in the modal and show it:
<!-- The Modal -->
<div id="myModal" class="modal">
<span class="close">×</span>
<img class="modal-content" id="myImg1">
<div id="caption"></div>
</div>
<div id="gallery">
<?php echo '<img src="'.esc_url( $info['0'] ).'" alt="Snow" style="width:100%;max-width:300px">'; ?>
</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 gallery = document.getElementById('gallery'),
modalImg = document.getElementById("myImg1");
gallery.addEventListener('click', function(e){
if(e.target.nodeName.toLowerCase() === 'img'){
modalImg.src = e.target.src;
modal.style.display = "block";
}
});
// 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>
You can also refactor a lot this piece of code... set an ID to modal, then only hook by tags, set the close event on the container and so on...
– n1kkou
Nov 13 at 13:31
Nothing happen on click.
– noufal
Nov 14 at 5:10
Yes, my bad, I updated the answer. I used "p" tag instead of "img"
– n1kkou
Nov 14 at 7:29
add a comment |
Do the following updates: wrap you new images in a container. Add the event listener to the container, match the tag you want(img) then update the src element in the modal and show it:
<!-- The Modal -->
<div id="myModal" class="modal">
<span class="close">×</span>
<img class="modal-content" id="myImg1">
<div id="caption"></div>
</div>
<div id="gallery">
<?php echo '<img src="'.esc_url( $info['0'] ).'" alt="Snow" style="width:100%;max-width:300px">'; ?>
</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 gallery = document.getElementById('gallery'),
modalImg = document.getElementById("myImg1");
gallery.addEventListener('click', function(e){
if(e.target.nodeName.toLowerCase() === 'img'){
modalImg.src = e.target.src;
modal.style.display = "block";
}
});
// 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>
Do the following updates: wrap you new images in a container. Add the event listener to the container, match the tag you want(img) then update the src element in the modal and show it:
<!-- The Modal -->
<div id="myModal" class="modal">
<span class="close">×</span>
<img class="modal-content" id="myImg1">
<div id="caption"></div>
</div>
<div id="gallery">
<?php echo '<img src="'.esc_url( $info['0'] ).'" alt="Snow" style="width:100%;max-width:300px">'; ?>
</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 gallery = document.getElementById('gallery'),
modalImg = document.getElementById("myImg1");
gallery.addEventListener('click', function(e){
if(e.target.nodeName.toLowerCase() === 'img'){
modalImg.src = e.target.src;
modal.style.display = "block";
}
});
// 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>
edited Nov 14 at 7:29
answered Nov 13 at 13:30
n1kkou
2,51821022
2,51821022
You can also refactor a lot this piece of code... set an ID to modal, then only hook by tags, set the close event on the container and so on...
– n1kkou
Nov 13 at 13:31
Nothing happen on click.
– noufal
Nov 14 at 5:10
Yes, my bad, I updated the answer. I used "p" tag instead of "img"
– n1kkou
Nov 14 at 7:29
add a comment |
You can also refactor a lot this piece of code... set an ID to modal, then only hook by tags, set the close event on the container and so on...
– n1kkou
Nov 13 at 13:31
Nothing happen on click.
– noufal
Nov 14 at 5:10
Yes, my bad, I updated the answer. I used "p" tag instead of "img"
– n1kkou
Nov 14 at 7:29
You can also refactor a lot this piece of code... set an ID to modal, then only hook by tags, set the close event on the container and so on...
– n1kkou
Nov 13 at 13:31
You can also refactor a lot this piece of code... set an ID to modal, then only hook by tags, set the close event on the container and so on...
– n1kkou
Nov 13 at 13:31
Nothing happen on click.
– noufal
Nov 14 at 5:10
Nothing happen on click.
– noufal
Nov 14 at 5:10
Yes, my bad, I updated the answer. I used "p" tag instead of "img"
– n1kkou
Nov 14 at 7:29
Yes, my bad, I updated the answer. I used "p" tag instead of "img"
– n1kkou
Nov 14 at 7:29
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53280096%2fimage-insert-inside-the-modal-popup%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
I'm assuming all your <img> tags have the id
myImg
, which is a conflict as many different elements are using the same id andgetElementById
is only attached to the first occurence. Learn More– Jaswinder Singh
Nov 13 at 11:48