Counter Click Javascript Jquery

Asked

Viewed 431 times

0

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

Imagens carregadas pelo Array

When I click I display the name of the image contained in the array. Below the image illustrates what happens when the event is held click, the names are displayed below the images, in the case ai only two images were clicked.

Array de nomes carregado após evento click

I have to count the clicks too. However I do not know how to perform the same event in the code below:

My html code looks like this:

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

And this is my jQuery with event click.

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

    I don’t understand the use of this line: let $span = $('img').append('<span>' + count + '</span>');... u want to make an append inside an image? An image is a closed element and does not accept append. Could explain better?

  • That line I forgot to take was a test I was performing. Ignore it.

  • I removed the answer. You have to adjust the question with the correct code. Noa comments you are taking and adding codes. You just have to edit the question with what you want and the correct code.

  • OK I’ll rephrase the question here.

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 event click.

var imageobject = [ { src: 'cat01.jpg', am: 'Cat 01', Count: 0, }, { src: 'cat02.jpg', am: 'Cat 02', Count: 0, }, { src: 'cat03.jpg', am: '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'));

})
  • Hernani, I understood it was better to create an array of objects same. I removed only the line $(this).after('<figcaption>' + imageObject[i].alt + '</figcaption>'); because the name should not be displayed before the click. Only click. Thanks bro. You saved the day, the year I mean. ;)

  • I kept this part of the code because the name is displayed after the click. $.each($('figure'), function(index, el) {&#xA; &#xA; $(this).one('click', function(event) {&#xA; / Act on the event /&#xA; $(this).append('<figcaption>' + image[index].alt+ '</figcaption>');&#xA; });&#xA; &#xA; });

  • So you could put this into the counting method because it will be executed when you click the same way and you wouldn’t need to run a $.each. When you listen to an event jquery gives you the element through $(this), then you don’t need a $each to listen to event..

  • Okay. Thank you bro.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.