0

Hello I am trying to add text to the picture if you click on it. At this moment if you click on a picture only Orange is displayed. What I would like to do is to add some text under the image when clicked only. Could somebody please help?

<a href="https://interactive-examples.mdn.mozilla.net/media/cc0-images/grapefruit-slice-332-332.jpg">
    <img alt="Qries" src="https://interactive-examples.mdn.mozilla.net/media/cc0-images/grapefruit-slice-332-332.jpg"
         width=150" height="70">
</a>

2 Answers 2

2

Something like this?

const res = document.getElementById("res");
document.querySelector("a").addEventListener("click", function(e) {
 e.preventDefault(); //stop link
  const tgt = e.target.closest("a");
  const img = tgt.querySelector("img")
  const text = img.alt;
  const src=img.src
  res.innerHTML = `<div style="text-align:center"><img src="${src}" /><br><span>${text}</span></div>`
  res.hidden=false;
})
 <a href="https://interactive-examples.mdn.mozilla.net/media/cc0-images/grapefruit-slice-332-332.jpg">
   <img alt="Qries Nice Grapefruit" src="https://interactive-examples.mdn.mozilla.net/media/cc0-images/grapefruit-slice-332-332.jpg" 
         width="150" height="70" /></a>
<div id="res" hidden></div>

Or this if a background image

const res = document.getElementById("res");
document.querySelector("div").addEventListener("click", function(e) {
  const src = this.style.backgroundImage.split('"')[1]

  const text = this.title;
  res.innerHTML = `<div style="text-align:center"><img src="${src}" /><br><span>${text}</span></div>`
  res.hidden = false;
})
<div title="Qries Nice Grapefruit" style="background-image:url('https://interactive-examples.mdn.mozilla.net/media/cc0-images/grapefruit-slice-332-332.jpg');     width:150px; height:70px" /></div>
<div id="res" hidden></div>

Sign up to request clarification or add additional context in comments.

4 Comments

This is perfect, but now I realise that my example was badly written because my image is in <div> not <img> tag and I can't figure it out.
So show the correct HTML and I will ad an example - update the question
See updated answer
stackoverflow.com/questions/69957976/… I've made another question
1

You Add a Little of css and Javascript to trick it for you

i have carefully added some comments to help you out in the process without much stress

//First i added some id to your html code to make it easier without just jumping to new files or opening some new links to make it bulky

var click = document.getElementById("image-to-click");
var text = document.getElementById("text-up");

//once the image is clicked, them the magic begins
click.onclick = function(){
  text.style.display = "block";
}
#text-up{
position: absolute; 
color: white;
padding: 40px;
display: none;
}


/* first, i have to remove the href to prevent it from opening another file on click. 
second, i added a text to display on the overlay */
<a id="image-to-click">
<span id="text-up">Text Ontop of the picture</span>
    <img alt="Qries" src="https://interactive-examples.mdn.mozilla.net/media/cc0-images/grapefruit-slice-332-332.jpg"
         width=350" height="150">
</a>

5 Comments

No need to remove the href, you can just prevent the link
yeah, using preventDefault; but absolutely no need of adding it in the first place
you do not know that. For SEO for example or to later change the page if clicking the popup
for SEO, i don't think adding an href to a image will actually boast your ranking or have any advantages instead it will cost more disadvantages because of unnecessary linking, so adding an href to where it's not required is doing more harm to your website than good
The bot will follow the link, so I disagree. Anyway. you do you, I do me.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.