It is possible to add click event on elements created dynamically ? <Read description>

Asked

Viewed 46 times

0

Hello good night to all!

I created elements in my HTML (span) and now I need to add a click event to display a sound whenever I click on some span tag that was created by Document.createelement but when I try the sound is added only when I create the direct span tag by HTML.

As you can see in the print below with the exception of the first, all other squares are created by Javascript with createelement, I would like to click on each square and play a sound that will be played randomly on each square.

The problem is that when I put the remote:

let sound = document.querySelectorAll('span');

This is only taking the first span that was created directly by my HTML and not an arrayLike so I can add click event with a foreach. Can someone help me? I am creating this just for study purposes, I thank all of you already!

Print: inserir a descrição da imagem aqui

  • 1

    See if this helps you: https://answall.com/a/50265/74

  • 1

    Particularly the part about delegation of events

  • 3

    "Display a sound" is a beautiful of a synesthesia.

  • @Piovezan, in the case did not say right but this "Display a sound" would take a part of the music I put in my HTML and activate with var.play() in the function.

  • @bfavaretto, I’ll give a read later, thanks for the indication!

No answers

Browser other questions tagged

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