Is it possible to pick up a title from an image using "Getelementsbytagname"?

Asked

Viewed 146 times

1

If yes, how? If not, is there any way I can get the title value and then print it? For now it is like this(Inside a function. In the image has an onClick calling the same).

var imagens = document.getElementsByTagName('img');
        document.getElementById('legenda').innerHTML = imagens;

I’ve tried for a getattribute too and nothing

1 answer

2

This shows the title of all images on the page:

var imagens = document.getElementsByTagName('img');
for(var i=0; i<imagens.length; i++) {
    alert(imagens[i].title)
}

Considering the code you included in the question, maybe this is what you want:

var imagens = document.getElementsByTagName('img');
// Joga A PRIMEIRA imagem encontrada na página para dentro da legenda
document.getElementById('legenda').appendChild(imagens[0]);
  • Yeah, that’s what I’m talking about! It is that I am trying to do that when an image is clicked, the title of it is printed on an id was trying to do with a getElementById but my doubt was what to put in the result... I tried more or less with what you said and I did not get result, even give "Undefined". Can you tell me how I’m going to be doing this? Thank you!

  • Can edit your question and include the code you already have? It’s hard to visualize just with what you said.

  • All right, I edited!

  • @Naldson It’s still not very clear what you’re trying to do, but I edited the answer based on your code.

  • It’s almost that. But with this way that you told me it prints the image that I clicked on. I wanted to print out her title.

  • document.getElementById('legenda').innerHTML = imagens[0].title;

Show 1 more comment

Browser other questions tagged

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