Display Image Alt Attribute Text On Hover - Stack Overflow
Có thể bạn quan tâm
-
- Home
- Questions
- Tags
- Users
- Companies
- Labs
- Jobs
- Discussions
- Collectives
-
Communities for your favorite technologies. Explore all Collectives
- Teams
Ask questions, find answers and collaborate at work with Stack Overflow for Teams.
Try Teams for free Explore Teams - Teams
-
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 CollectivesTeams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
Learn more about TeamsGet 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 2I'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 MartinMartin 1433 gold badges6 silver badges13 bronze badges Add a comment |2 Answers
Sorted by: Reset to default Highest score (default) Trending (recent votes count more) Date modified (newest first) Date created (oldest first) 5I'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 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
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 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 discardedSign up or log in
Sign up using Google Sign up using Email and Password SubmitPost as a guest
Name EmailRequired, but never shown
Post Your Answer DiscardBy 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
Related
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 jQueryHot 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)
To subscribe to this RSS feed, copy and paste this URL into your RSS reader.
defaultTừ khóa » Html Image Alt Text Hover
-
Why An HTML Image Alt Text Doesn't Show On Hover - Computer Hope
-
HTML Img Alt Attribute - W3Schools
-
Should The Alt Attribute Show Up When Hovering On The Image?
-
Show Img Alt Text On Hover - CodePen
-
Alt Text Doesn't Appear On Hover - Finalsite Support
-
Image ALT Tag Tips For HTML - Accessibility At Penn State
-
How Do I Show Alt Text When Hovering Over An Anchor With A Mouse?
-
Hide Image Title And Alt Text On Hover - GeneratePress
-
HTML Img Alt Tags For SEO Best Practice - Search Engines Love ...
-
How Do I Add Hover Text To An Image I Have | Forums
-
Topic: Alt-text Or Title Do Not Appear When You Hover Over An Image
-
Image Alt Text In Source But Not Displaying
-
How To View The Alt Text When Hover Over An Image
-
Why An HTML Image Alt Text Doesn't Show On Hover - Support