1
How to take the title of an image and display in a alert
?
1
How to take the title of an image and display in a alert
?
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 javascript
You are not signed in. Login or sign up in order to post.
Where is the title? In your HTML? Please include html in the question.
– bfavaretto