Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

204
Views
How do I have a JavaScript bookmarklet display a tooltip on keyboard focus?

I have a JavaScript bookmarklet which copies alt attributes to title attributes to allow editors who don't use screen readers to audit their alt text. The alt text (or lack thereof) appears as a browser tooltip on hover.

javascript:

(function () {
  var imgs = document.getElementsByTagName("img");
  for (i = 0; i < imgs.length; i++) {
    if (imgs.item(i).hasAttribute("alt")) {
      if (imgs.item(i).getAttribute("alt") == "") {
        imgs.item(i).setAttribute("title", " empty alt attribute ");
        imgs.item(i).style.border = "2px dotted black";
      } else if (imgs.item(i).getAttribute("alt") == " ") {
        imgs.item(i).setAttribute("title", " space alt attribute ");
        imgs.item(i).style.border = "2px dotted orange";
      } else {
        imgs.item(i).setAttribute("title", imgs.item(i).getAttribute("alt"));
        imgs.item(i).style.border = "2px dashed green";
      }
    } else {
      imgs.item(i).setAttribute("title", " no alt attribute ");
      imgs.item(i).style.border = "2px solid red";
    }
  }
  /* released under GPL 3.0 */
})();

I would like to revise the bookmarklet to make it accessible to editors who can only use a keyboard. I am able to add focus to all images, but when I execute the bookmarklet on a page and tab the focus to an image, the tooltip doesn't appear.

javascript:

(function () {
  var imgs = document.getElementsByTagName("img");
  for (i = 0; i < imgs.length; i++) {
    if (imgs.item(i).hasAttribute("alt")) {
      imgs.item(i).setAttribute("tabIndex", "0");
      if (imgs.item(i).getAttribute("alt") == "") {
        imgs.item(i).setAttribute("title", "*** empty alt attribute ***");
        imgs.item(i).style.border = "2px dotted black";
      } else if (imgs.item(i).getAttribute("alt") == " ") {
        imgs.item(i).setAttribute("title", "*** space alt attribute ***");
        imgs.item(i).style.border = "2px dotted orange";
      } else {
        imgs.item(i).setAttribute("title", imgs.item(i).getAttribute("alt"));
        imgs.item(i).style.border = "2px dashed green";
      }
    } else {
      imgs.item(i).setAttribute("title", "*** no alt attribute ***");
      imgs.item(i).style.border = "2px solid red";
    }
  } /* released under GPL 3.0 */
})();

Is there any way to make the tooltip appear on focus?

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!