2
I’m trying to add a few songs to one playlist, but I can’t figure out which song was clicked to add.
HTML
<div class='album-musicas'>
<a href='#' id='add-musica-playlist-link' class='addMusicaPlaylistLink'>
<input type='hidden' id='id-musica' value='1' name='dados'>
<input type='hidden' id='nm-musica' value='Musica 1' name='dados'>
<input type='hidden' id='nm-cantor' value='Cantor 1' name='dados'>
<img src='images/button-add-13.png' title='Adicionar à playlist' >
</a>
</div>
<div class='album-musicas'>
<a href='#' id='add-musica-playlist-link' class='addMusicaPlaylistLink'>
<input type='hidden' id='id-musica' value='2' name='dados'>
<input type='hidden' id='nm-musica' value='Musica 2' name='dados'>
<input type='hidden' id='nm-cantor' value='Cantor 2' name='dados'>
<img src='images/button-add-13.png' title='Adicionar à playlist' >
</a>
</div>
Javascript
$(document).on('click', '#add-musica-playlist-link', function () {
var botoes = document.getElementsByClassName("addMusicaPlaylistLink");
for(var i = 0; i < botoes.length; i++) botoes[i].addEventListener("click", function(e){
var musica_id = e.srcElement.getElementsByTagName("input")[0].value;
alert(musica_id);
});
});
e.srcElement
only works on IE (and I don’t even know if it still works on the new ones). This is the browser you’re testing on?– bfavaretto
No, I’m testing on Firefox
– Ricardo
Then change
e.srcElement
fore.target
. I think that’s enough to work on the FF.– bfavaretto
Now that I’ve noticed you’re using jQuery. I’ll post an answer simplifying your code.
– bfavaretto