Paste HTML element via Javascript using getElementsByClassName

2

What is the problem with this javascript code? The function always returns error.

<!-- IMAGEM -->
<img class="coverPhotoImg photo img" src="ABC.JPG" alt="Foto 1">
<!-- IDENTIFICAÇÃO --->
<span class="hidden_elem enableFriendListFlyout outgoingButton" data-profileid="2015">Protocolo enviado.</span>
<script type="text/javascript">
//PELA LOGICA DEVERIA RETORNAR ABC.JPG
window.alert(document.getElementsByClassName('coverPhotoImg photo img').getAttribute('src'));
//PELA LOGICA DEVERIA RETORNAR 2015
window.alert(document.getElementsByClassName('hidden_elem enableFriendListFlyout outgoingButton').getAttribute('data-profileid'));
</script>
    
asked by anonymous 26.07.2015 / 00:37

1 answer

4

The getElementsByClassName always returns a list of the elements it found, even if only one, the correct code is:

document.getElementsByClassName("coverPhotoImg photo img")[0].getAttribute("src")
    
26.07.2015 / 00:52