How to get the id of each link in the click? Only the first id comes

1

How do I get data-id of all links? Only the first one is coming, repeated.

[![$("#filmeItem").on("keyup focusin", function (e) {
        $('.livesearchItem').fadeIn('1000');
        e.preventDefault();
        var busca = $('#filmeItem').val();
        if (busca !== "") {
            var settings = {
                'async': true,
                'crossDomain': true,
                'url': 'https://api.themoviedb.org/3/search/movie?query=' + busca + '&api_key=9aca69849a23528a419aea463387945f&language=pt-BR',
                'method': 'GET',
                'headers': {},
                'data': '{}'
            };
            $.ajax(settings).done(function (data) {
                $.each(data.results, function (key, value) {
                    if (value.poster_path !== null) {
                        $('.livesearchItem').append('<a href="#" class="itn" id=' + value.id + '><div class="media"><br><div class="media-left media-middle"><img class="media-object img-thumbnail" src="https://image.tmdb.org/t/p/w92'+value.poster_path+'" width="45" height="45" alt="..."></div><div class="media-body alinhamentom"><h4 class="media-heading">' + value.title + '</h4>' + moment(value.release_date).format('Y') + '<span class="label label-success pull-right">FILME</span></div></div></a>');
                    } else {
                        $('.livesearchItem').append('<a href="#" class="itn" id=' + value.id + '><div class="media"><br><div class="media-left media-middle"><img class="media-object img-thumbnail" src="' + baseUrl + 'assets/images/img-not-found.jpg" width="45" height="45" alt="..."></div><div class="media-body alinhamentom"><h4 class="media-heading">' + value.title + '</h4>' + moment(value.release_date).format('Y') + '<span class="label label-success pull-right">FILME</span></div></div></a>');
                    }
                });
            });
        }
        $('.livesearchItem').empty();
    });

    $('.livesearchItem').on('click', function () {
        console.log($('.itn').attr('id'));][1]][1]

** Update: **. I solved the problem by using the "dataset"

    
asked by anonymous 05.04.2017 / 18:24

1 answer

1

Assuming the code to be:

<elemento class="itn" id="valor1" />
<elemento class="itn" id="valor2" />
<elemento class="itn" id="valor3" />

Enough to get the list of ids, in a separate array would be:

var lResult = new Array();
        $.each($(".teste"), function (indice, obj) {
            return lResult.push($(obj).attr("id"));
        });;

What should be added within a function, or event or in the document load, depends on the moment you want to get that information.

    
05.04.2017 / 19:24