Display Image Alt Attribute Text On Hover - Stack Overflow

    1. Home
    2. Questions
    3. Tags
    4. Users
    5. Companies
    6. Labs
    7. Jobs
    8. Discussions
    9. Collectives
    10. Communities for your favorite technologies. Explore all Collectives

  1. Teams

    Ask questions, find answers and collaborate at work with Stack Overflow for Teams.

    Try Teams for free Explore Teams
  2. Teams
  3. Ask questions, find answers and collaborate at work with Stack Overflow for Teams. Explore Teams

Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

Get early access and see previews of new features.

Learn more about Labs Display image Alt Attribute text on hover Ask Question Asked 8 years, 8 months ago Modified 8 years, 8 months ago Viewed 13k times 2

I'd like to know how to display alt text of img, inside of p element on mouse hover.

Here's my HTML:

<img src="img1.jpg" alt="one"/> <img src="img2.jpg" alt="two"/> <img src="img3.jpg" alt="three"/> <p id="text"> </p>

And JS:

function showAlt(){ var img = document.getElementsByTagName("img"); var text = document.getElementById("text"); for(var i=0; i < img.length; i++){ img[i].addEventListener("mouseover", function(){ var alt = img[i].alt; alt.appendChild(text);}} }

And JSfiddle: here

Share Improve this question Follow asked May 2, 2016 at 22:43 Martin's user avatar MartinMartin 1433 gold badges6 silver badges13 bronze badges Add a comment |

2 Answers 2

Sorted by: Reset to default Highest score (default) Trending (recent votes count more) Date modified (newest first) Date created (oldest first) 5

I'm not sure of the context for this, but the title="" attribute is often shown as a tooltip for images. Should work in all of the desktop browsers, so I'm not sure if you need JS to do this.

Share Improve this answer Follow answered May 2, 2016 at 22:48 Nick Goodrum's user avatar Nick GoodrumNick Goodrum 4012 silver badges7 bronze badges 2
  • That's not what he's asking. He wants to display the alt text in another HTML element when hovered on.user663031 Commented May 3, 2016 at 2:06
  • 4 Thanks, I'm aware that his setup is slightly different. Just giving an alternative if they weren't already aware. The title was more general so it might have been a helpful alternative. – Nick Goodrum Commented May 3, 2016 at 2:33
Add a comment | 2

You can change text of p element with textContent DEMO

var img = document.getElementsByTagName("img"); var text = document.getElementById("text"); for (var i = 0; i < img.length; i++) { img[i].addEventListener("mouseover", function() { var alt = this.alt; text.textContent = alt; }); }

If you want to remove text on mouseleave you can add another event and change text to empty string DEMO

Share Improve this answer Follow edited May 2, 2016 at 22:57 answered May 2, 2016 at 22:47 Nenad Vracar's user avatar Nenad VracarNenad Vracar 122k16 gold badges159 silver badges183 bronze badges 0 Add a comment |

Your Answer

Reminder: Answers generated by artificial intelligence tools are not allowed on Stack Overflow. Learn more

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid …

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.

Draft saved Draft discarded

Sign up or log in

Sign up using Google Sign up using Email and Password Submit

Post as a guest

Name Email

Required, but never shown

Post Your Answer Discard

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.

  • The Overflow Blog
  • Developers want more, more, more: the 2024 results from Stack Overflow’s...
  • How AI apps are like Google Search
  • Featured on Meta
  • The December 2024 Community Asks Sprint has been moved to March 2025 (and...
  • Stack Overflow Jobs is expanding to more countries
6137 How to disable text selection highlighting 2271 Vertically align text next to an image? 2305 HTML: Valid id attribute values? 2123 How to auto-resize an image while maintaining aspect ratio 2004 Transitions on the CSS display property 2131 How can I display a JavaScript object? 1596 How do I vertically align text in a div? 2158 Preview an image before it is uploaded 2543 Get selected text from a drop-down list (select box) using jQuery

Hot Network Questions

  • What happened to 1T-SRAM?
  • What does "standard bell" mean?
  • First instance of the use of immersion in a breathable liquid for high gee flight?
  • What is the origin of "litera" versus "littera"?
  • Is it possible to discover a "Universal formula" that generates and generalizes all odd Collatz numbers?
  • Is there any denomination which officially rejects Young Earth Creationism?
  • How to swim while carrying fins (i.e., when the fins aren't positioned on my feet)?
  • Why does capacitive coupling require a base resistor in an emitter follower?
  • Manathermy: effects on the ecosystem
  • Does gravity from a star go through a black hole's event horizon to affect objects on the other side?
  • How to model a stone sauna stove for game asset?
  • Bringing in a peanut butter sandwich to discourage lunch thief who has peanut allergy
  • Do I need a publisher in order to make a physical book?
  • "Sacks of which articles" in "Wuthering Heights"
  • What is Application Manager for macOS, and what does it do?
  • Why didn't Kafka use quotation marks in this dialogue?
  • Algebraic equation to represent a triangle.
  • Connections between the path integral formulation and the Fourier transform
  • How does FM preemphasis interact with maximum deviation?
  • Are there any languages without adpositions?
  • Did a peaceful reunification of a separatist state ever happen?
  • What use is SPF for email security in a cloud / SAAS world
  • Are NASA computers really that powerful?
  • How can I document that I am allowed to travel to the UK from Scandinavia using eVisa/BRP? (Denied at the check-in counter)
more hot questions Question feed Subscribe to RSS Question feed

To subscribe to this RSS feed, copy and paste this URL into your RSS reader.

default

Từ khóa » Html Image Alt Text Hover