Why is jquery returning repeating information?

Asked

Viewed 48 times

0

I’m using the command

$("div").delegate( ".btnAcessoAtividade", "click", function()
{ 
    var id =  $(this).attr('id');                                                                     
    window.localStorage.setItem("id",id);
    console.log( $(this) );
    $('.reunioes').prepend( window.localStorage.getItem('id') );});

because I want to get the id of the following structure:

$(".reunioes").append(
    "<div class='btnAcessoAtividade' id ='*"+response.data[i].id+"'>"
    +"<span class='nome'> "+response.data[i].deal_title+"</span>"
    +"<span class='hora'>"+response.data[i].due_time+"</span>"
    +"</div>"
);

only that for some reason, it’s bringing the "id" 3 times...:

init [div#*14174.btnAccessiveness, context: div#*14174.btnAccessActivity] init [div#*14174.btnAccessiveness, context: div#*14174.btnAccessiveness] init [div#*14174.btnAccessiveness, context: div#*14174.btnAccessActivity]

why does it happen? what am I doing wrong?

  • 1

    Because you have three elements with the btnAccessAtivity class?

  • hi, Leandro, did not understand its placement...I have the main class , that is the "meetings", within it, are several classes(elements) "btnAccessAtivity" as if they were "lines"...each line, with its id...but the way it appears and that I posted,who repeats, are the ids ids

  • It is difficult to specify your error exactly without the rest of the code and HTML. You can get an idea of the error but without precision. If you have a for, you are probably applying the same ID to several buttons. Or you have more than one class .reunioes (in case, 3).

1 answer

0

Try adding the Event Listener to each element, not the return list with multiple elements. I’m using on as an example because delegate is already out of date.

$("div .btnAcessoAtividade").each(function(i, el) {
    $(el).on('click', clickBtnAcessoAtividade);
});

function clickBtnAcessoAtividade(evt) {
    var id =  $(this).attr('id');                                                                     
    window.localStorage.setItem("id",id);
    console.log( $(this) );
    $('.reunioes').prepend( window.localStorage.getItem('id'));
}

Browser other questions tagged

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