0
I am developing a project where creates dynamically, several div with the class "musicItem" and for each div I have associated a data-musicId, and also I have a click on this div that when clicking I do some actions but for this I need to know the "data-musicid", the problem is that if I click on the div everything happens perfectly, but if you click on some child element of it gives error. It would be possible to solve this problem with javascript vanilla, without jquery?
Click detection code in the div:
const musicItens = document.querySelectorAll('.musicItem');
for (const music of musicItens) {
music.addEventListener('click', function(event) {
const clickedmusic = event.target.attributes['data-musicid'].value;
this.play(clickedmusic);
}.bind(this));
}
Sons of the Div:
<div class="musicItem" data-musicid="1">
<div class="musicPhoto">
<img src="assets/images/3348986.jpg">
</div>
<div class="musicData">
<p>Malha Suprema - Judas</p>
<p>ACERT</p>
</div>
<div class="btnPlayer">
<button class="playerBtn">
<span class="playerImg">
<i class="far fa-play-circle"></i>
</span>
</button>
</div>
</div>
Guy from what I understand you want to click on an element with the class
musicItem
you want to know thedata-musicid
and give a.play()
in it?– hugocsl
Yes call the play function()
– Tomás