How to get the title of an image by Javascript?

Asked

Viewed 664 times

1

How to take the title of an image and display in a alert?

  • 3

    Where is the title? In your HTML? Please include html in the question.

2 answers

9

you only need to select the image (either by ID or other selector), then access the property title.

var outraDiv = document.getElementById("outra-div");
var imagem = document.getElementById("imagem");
imagem.addEventListener("click", function (event) {
    outraDiv.textContent = imagem.title;
});
<div id="outra-div"></div>
<img id="imagem" src="http://image005.flaticon.com/1/svg/72/72716.svg" title="Award" />

  • I understood, and if in case I want to take the title, only when the image is clicked, and then display this title in a div, for example.

  • @Thiagosilva, in this case you will also need to take this div by some selector, then add an Ejust to the image, during the event you update the content of the div.

  • has how you post the example of a type code, because I don’t know much of javascript. I thank you

  • @Thiagosilva, had already updated the answer with the requested code 23 hours

1

You can also do inline

<div id="div"></div>
<img src="" id="imagem" onclick="document.getElementById('div').innerHTML=this.title" title="título" />

And if you have many Ivs you can do by function

 <script> 
function titulo(div,titulo) {
 document.getElementById(div).innerHTML=titulo; } 
</script>

 <div id="div"></div> <img src="" id="imagem" onclick="titulo('div',
 this.title)" title="título" />

Browser other questions tagged

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