Jquery JavaScript Click Counter

0

I'm applying image attributes and names via Jquery through an array of images and names. The image below illustrates the images of the loaded array.

WhenIclickondisplaythenameoftheimagecontainedinthearray.Belowtheimageillustrateswhathappenswhentheclickeventisperformed,thenamesaredisplayedunderneaththeimages,incaseonlytwoimageshavebeenclicked.

Ihavetocounttheclicksaswell.ButIdonotknowhowtoperformthissameeventinthecodebelow:

Myhtmlcodelookslikethis:

<divclass="col-md-3"> <figure class="catImag"><img src=""></figure> <span class="count"></span> onde vai o contador </div>

And this is my jQuery with click event.

 const catsImages = ['cat01.jpg', 'cat02.jpg', 'cat03.jpg', 'cat04.jpg', 'cat05.jpg'];
    const catsName = ['Cat KiKI', 'Cat Edi', 'Cat Didi', 'Cat Kely', 'Cat Vivi'];

    let image = [...catsImages];
    let name = [...catsName];

    console.log(image);
    console.log(name);

    let count = 0;//Variavel contador

    $.each($('.catImag img'), function(index) {
        $(this).attr({
            src: image[index],
            alt: name[index]
        });
    }); //Update Cat cria os atributos para receber a imagem

    $.each($('figure'), function(index, el) {

        $(this).one('click', function(event) {
            /* Act on the event */
            $(this).append('<figcaption>' + name[index] + '</figcaption>');
        });

    });// update click name, exibe o nome do cat clicado
    
asked by anonymous 18.12.2018 / 17:52

1 answer

1

Man tries something like this:

''         

    </figure>
    <figure class="catImag">
    <img src="" alt="">

    </figure>
    <figure class="catImag">
    <img src="" alt="">

''

And this is my jQuery with click event.

'' var imageObject = [         {             src: 'cat01.jpg',             alt: 'Cat 01',             count: 0,         },         {             src: 'cat02.jpg',             alt: 'Cat 02',             count: 0,         },         {             src: 'cat03.jpg',             alt: 'Cat 03',             count: 0,         },     ];

$ ('. catImag img'). each (function (i, el) {

$ (this) .attr (      {             'src': imageObject [i] .src,             'alt': imageObject [i] .alt,             'data-count': imageObject [i] .count         }  );

$ (this) .after ('' + imageObject [i] .alt + ''); $ (this) .after ('');

})

$ ('. catImag') .click (function () {

var countIni = $(this).children('img').attr('data-count');

$(this).children('span').text(countIni = parseInt(countIni, 10) + 1);
$(this).children('img').attr('data-count',countIni)


console.log($(this).children('span'));

})

''

It was kind of bad here, I can not publish the stops here yet .. Here's an example: insert the link description here

    
19.12.2018 / 13:05