Pick up dynamically created element

Asked

Viewed 789 times

1

I am generating a dynamically A tag in a pure AJAX file, without Jquery. The code:

 if(xhr.status == 200){

  console.log(xhr.responseText);
  let resposta = xhr.responseText;
  resposta = JSON.parse(resposta);

  let x = document.querySelector('#resposta')
  resposta.map(item =>{

    x.innerHTML += '<a value='+ item.id +' class="descricao-resposta" 
  id="resposta-selecionada">'+ item.descricao +'</a>'
  });

My problem is that I need to capture this A tag later to do an event with it, I’m trying to capture this way.

var btnResponde = document.querySelector('#resposta-selecionada');
btnResponde.addEventListener('click', function(evento){

evento.preventDefault();
respondeExercicio();
});

But the console says that my addEventeListener is coming null, IE, is not finding the code that I am generating dynamically. How can I do that?

  • You are creating more than one right button?

  • I’m actually only creating the A tag and trying to capture it after created with addEventeListener @Wictorchaves

  • 1

    Remember that you cannot have 2 identical id’s in html, so if the code inside if(xhr.status == 200){ Running twice won’t work as expected. Just as it is necessary to give extra behavior in relation to the click, to elements that are later added.

  • At what point do you add the event? Why not add it inside the resposta.map?

1 answer

0


I made a small example to better exemplify, this way the event works normally:

let x = document.querySelector('#resposta');

x.innerHTML = '<a href="#" data-value="1" class="descricao-resposta" id="resposta-selecionada">teste</a><br/>';

var btnResponde = document.querySelector('#resposta-selecionada');

btnResponde.addEventListener('click', function(evento){
    evento.preventDefault();
    respondeExercicio();
});

function respondeExercicio(){
    alert('ok');
}

Upshot: https://jsfiddle.net/uts27ejk/7/

Now I’m adding two buttons:

let x = document.querySelector('#resposta');

x.innerHTML = '<a href="#" data-value="1" class="descricao-resposta" id="resposta-selecionada">teste</a><br/>';
x.innerHTML += '<a href="#" data-value="1" class="descricao-resposta" id="resposta-selecionada">teste</a><br/>';

var btnResponde = document.querySelector('#resposta-selecionada');

btnResponde.addEventListener('click', function(evento){
    evento.preventDefault();
    respondeExercicio();
});

function respondeExercicio(){
    alert('ok');
}

You can notice, that the second button does not work, this is because there cannot be two buttons with the same id, so it only assigns the event to the first button.

Upshot: https://jsfiddle.net/uts27ejk/9/

You can solve this way:

let x = document.querySelector('#resposta');

x.innerHTML = '<a href="#" data-value="1" class="descricao-resposta" >teste</a><br/>';
x.innerHTML += '<a href="#" data-value="1" class="descricao-resposta" >teste</a><br/>';

var elementos = document.querySelectorAll('.descricao-resposta');

[].forEach.call(elementos, function(elemento) {
  elemento.addEventListener('click', function(evento){
      evento.preventDefault();
      respondeExercicio();
  });
});

function respondeExercicio(){
    alert('ok');
}

Upshot: https://jsfiddle.net/uts27ejk/18/

So you use only the class (since it can be repeated) makes a foreach to go through all the elements and assign the desired event.

  • But I’m only creating one and it’s not giving

  • But what is happening, the button does not appear, or it does not execute the click event?

  • It is generated, it appears on the front, but I cannot select with Document.querySelecotr... which is the second part of the code I posted

  • What do you mean "select"?

  • Like, when I do this code to put the action on the button that I’m generating with the A tag, it says that the event comes null... it’s not finding the tag that’s being created, even though it appears on the screen.. var btnResponde = Document.querySelector('#response-selected'); btnResponde.addeventlistener('click', Function(event){ event.preventDefault(); respondeExercicio(); ;});

  • 1

    Have you tried to assign this code under "x.innerHTML ...", I do not know how this the order of your original code, but you are receiving information, this information can be coming asynchronously, IE, you can be assigning the event even before it has been created, then placing below where it is being created the problem can be solved.

  • 1
Show 2 more comments

Browser other questions tagged

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