How to dispatchEvent key up in a for loop and waiting for a dom search reaction to put the resulted element in an array

49 Views Asked by At

I would be gratteful if someone could give me some clue. I'm trying to dispatch Key up event in a input element after fill in it. Everything is in the Dom. The line code is in a loop. So it gives me something like this :

function makeSomeAction()
{
  var arrayOfElementsNameToClick = []; 

  for( const Name of arrayOfNames){
  myElementInput.value = Name;
  myElementInput.dispatchEvent(new Event('keyup'));
  var myResultOfSearch = document.querySelector("#blabla");
  arrayOfElementsNameToClick.push(myResultOfSearch); 

  }
  
  return arrayOfElementsNameToClick; 

}

When i try this piece of code, it's working only for the last Name of arrayOfNames. I also tried to trigger the dispatch Event Element with an addEventListener. but it workking on the same way (last name of the array). It's like i should wait that the dom gives me some result to go to the next iteration of the for loop. The problem is that the dispatch event is not fired, and i can't access to the element of the search. Only the last element of my arrayOfNames.

What i expect is that the for loop

  • load the right name on the input element,
  • wait that the event has been dispatch on this element,
  • and then takes the element from the dom with the usual querySelector and put it into an array
  • after this only, it go throught the next iteration.

Here's a picture to illustrate my explanation:

enter image description here enter image description here

I've already tried to addEventListener inside the loop without succeed. If someone could help me, i would be verry gratefull.

0

There are 0 best solutions below