Image insert inside the modal popup












2














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">&times;</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>









share|improve this question
























  • 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


















2














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">&times;</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>









share|improve this question
























  • 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
















2












2








2







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">&times;</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>









share|improve this question















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">&times;</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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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 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


















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














3 Answers
3






active

oldest

votes


















1














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";
}
}





share|improve this answer





























    2














    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">'; ?>





    share|improve this answer





















    • 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



















    0














    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">&times;</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>





    share|improve this answer























    • 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











    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
    });


    }
    });














    draft saved

    draft discarded


















    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









    1














    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";
    }
    }





    share|improve this answer


























      1














      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";
      }
      }





      share|improve this answer
























        1












        1








        1






        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";
        }
        }





        share|improve this answer












        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";
        }
        }






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 14 at 6:02









        Noufal Binu

        9010




        9010

























            2














            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">'; ?>





            share|improve this answer





















            • 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
















            2














            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">'; ?>





            share|improve this answer





















            • 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














            2












            2








            2






            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">'; ?>





            share|improve this answer












            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">'; ?>






            share|improve this answer












            share|improve this answer



            share|improve this answer










            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


















            • 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











            0














            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">&times;</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>





            share|improve this answer























            • 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
















            0














            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">&times;</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>





            share|improve this answer























            • 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














            0












            0








            0






            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">&times;</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>





            share|improve this answer














            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">&times;</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>






            share|improve this answer














            share|improve this answer



            share|improve this answer








            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


















            • 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


















            draft saved

            draft discarded




















































            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.




            draft saved


            draft discarded














            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





















































            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







            Popular posts from this blog

            Guess what letter conforming each word

            Port of Spain

            Run scheduled task as local user group (not BUILTIN)