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

Asked

Viewed 484 times

1

How to catch the data-id of all links? Only the first, 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 using "dataset".

  • What are all links? All the tags?

  • Are a livesearch. Image, title, etc.

1 answer

1

Assuming the code is:

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

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

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

Which should be added within a function, event or Document load, depending on when you want to get this information.

  • It seems that the problem is that I’m putting the "data-id" inside an append(), so he just finds the first one. I am trying to get the id of the movies inside a livesearch with a click, it is possible to do this?

  • I think now I understand what you want to do, @Cyrofarias. I edited the answer. - And the question.

Browser other questions tagged

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