Modal does not work

I did modal using jQuery, but it does not work properly. I call my images in a javascript function, and modal in my window.onload on the same page where the images are loaded. First I load the image, but the modal only works if I put imgs directly in HTML, and if I take the rest of the window.onload. This is what I call all imgs:

function maisGaleria(n){

    var galeria = new Array();
    var img = document.querySelector("#gallery");
    var pic = 0;

        if(n == 1){
            pic = 4;
        }else if(n == 2){
            pic = 7;
        }else{
            pic = 4;
        }

        img.innerHTML = "<div class='row'>";
        img.innerHTML += "<div class='six columns'>";
        img.innerHTML += "<h4>Galeria "+n+"</h4>";

        for(var i = 0; i < pic; i++){
            galeria[i] = "foto"+n+"_"+(i+1)+".jpg";
        }

        for(var i = 0; i < galeria.length; i++){
            img.innerHTML += "<img src='img/"+galeria[i]+"' class='imgs-modal'>";
        }

        img.innerHTML += "</div>";
        img.innerHTML += "</div>";
}

and window.onload with modal:

        window.onload = function(){

        var rdSocial = document.querySelector("#social");
        var teste = document.querySelector("#teste");

            var fb = '<img src="img/fb.png" value="1" class="img" onclick="redireciona(this);">';
            var twt = '<img src="img/twitter.png" value="2" class="img" onclick="redireciona(this);">';
            var insta = '<img src="img/insta.png" value="3" class="img" onclick="redireciona(this);">';

            rdSocial.innerHTML += fb;
            rdSocial.innerHTML += twt;
            rdSocial.innerHTML += insta;

            var x = location.search.split("?gal=")[1];

            y = document.querySelector("#pics");

            //console.log(x);

            y.onload = maisGaleria(x);

//the modal starts here
                    $('.imgs-modal').on('click', function(){

                        var obj = $(this).clone();
                        console.log(obj);

                        $("#box_modal").html(obj);
                        $("#modal").fadeIn(1000);                   
                    });

                    $(".fechar").on('click', function(){
                        $("#modal").fadeOut(1000);
                    });

    }
+4
source share
1 answer

@anuseranother there are several errors in your code.

Before calling the maisGaleria method in window.onload you must declare / define it.

jsfiddle, , maisGaleria . maisGaleria .

: " " onload " null". dom #pics (y = document.querySelector( "# pics" )), onload. , , .

+1

All Articles