39

I have an anchor element with a title attribute. I want to hide the popup that appears when hovering over it in the browser window. In my case, it is not possible to do something like this,

$("a").attr("title", "");

Because of jQuery Mobile the title will reappear after certain events occur (basically everytime the anchor element gets redrawn). So I hope to hide the title via CSS.

Something like:

a[title] {
    display : none;
}

doesn't work, since it hides the entire anchor element. I want to hide the title only. Is this even possible? The popup shouldn't display.

Damjan Pavlica
  • 31,277
  • 10
  • 71
  • 76
jtepe
  • 3,242
  • 2
  • 24
  • 31
  • 1
    no, you can't hide the tooltips. that's not part of the css spec. – Marc B Mar 12 '13 at 14:32
  • possible duplicate of [How to disable tooltip in the browser with jQuery?](http://stackoverflow.com/questions/1027762/how-to-disable-tooltip-in-the-browser-with-jquery) – andyb Mar 12 '13 at 14:33
  • That's too bad. I will have to find something else then. – jtepe Mar 12 '13 at 14:34
  • @Gatekeeper It's a tooltip which is created by jQuery Mobile, so I'm pretty much stuck with it. I just want it to hide :) – jtepe Mar 12 '13 at 14:44
  • 1
    Another idea (much less elegant than what you're looking for, but gets the job done): You could change the behavior of jQuery Mobile. I wouldn't directly change the source (not generally good practice), but you could write an extension that hooks in and removes the title after it renders the elements. – Aaron Blenkush Mar 12 '13 at 14:49
  • You say it's not possible to do `$("a").attr("title", "");` in your case. Is this because you have some constraints, or because you tried and it didn't work? If it just didn't work, you may instead need to do `$('a').prop('title', '');` since `attr` != `prop` in jQuery >= 1.6.. – Noyo Mar 12 '13 at 14:51
  • @Aaron Yep, that's what I did. My plugin creates a slider that steps in hexadecimal steps. I registered handlers for 'slidestop' and 'mouseover', hiding the title attribute, but while dragging the slider it still shows. – jtepe Mar 12 '13 at 14:52
  • @Noyo Contraints. I did the $("a").attr("title", "") in a lot of events, but there are still some in which I cannot hook into. See my answer to Aaron. – jtepe Mar 12 '13 at 14:54
  • In which events? `slidestart` and `slidestop`? – Noyo Mar 12 '13 at 15:00
  • Do you by any chance know how the plugin creates this tooltip? If so you may be able to target that and hide it instead of the `a`. – BoltClock Mar 12 '13 at 15:10
  • @BoltClock jQuery Mobile updates it everytime in it's refresh method, so it's not part of the plugin. – jtepe Mar 12 '13 at 15:19
  • @Noyo Both `slidestop` and `slidestart` – jtepe Mar 12 '13 at 15:22

8 Answers8

31

Using the following CSS property will ensure that the title attribute text does not appear upon hover:

pointer-events: none;

Keep in mind that JS is a better solution since this CSS property will ensure that the element is never the target of any mouse events.

Blake Frederick
  • 1,510
  • 20
  • 31
  • 18
    This will also disable any functions following clicking on the link. Which renders the whole link pointless. – Hafenkranich Jul 04 '17 at 15:41
  • 3
    Yes @Hafenkranich, that's why in my answer I wrote "Keep in mind that JS is a better solution since this CSS property will ensure that the element is never the target of any mouse events." – Blake Frederick Jul 05 '17 at 20:14
  • In Safari the title keeps showing up, even with `pointer-events: none` – Frank Adrian May 15 '23 at 08:46
15

You can wrap your inner text in a span and give that an empty title attribute.

<a href="" title="Something">
  <span title="">Your text</span>
</a>
vsync
  • 118,978
  • 58
  • 307
  • 400
Arjan Frans
  • 481
  • 4
  • 10
12

As per @boltClock's suggestion, I'll say I don't feel that a CSS solution is appropriate here, as the browser decides what to do with the title attribute of a link, or anything for that matter. CSS, to my knowledge, is unable to handle this issue.

As mentioned, using jQuery to replace the title with an empty string wont work because jQuery mobile rewrites them at some points. This, however, will work independently of JQM, and doesn't involve entirely removing the title attribute which is SEO important.

This works:

$('a["title"]').on('mouseenter', function(e){
    e.preventDefault();
});

I changed my initial code of $('body').on('mouseenter') to this after testing. This is confirmed to work.

brasofilo
  • 25,496
  • 15
  • 91
  • 179
Todd
  • 5,314
  • 3
  • 28
  • 45
  • Moving my comment from the other answer to here: this doesn't look like CSS to me. – BoltClock Mar 27 '13 at 06:02
  • 2
    well, @BoltClock, that's because it isn't. the OP only said that something like the jquery script HE/SHE included wouldn't work. Because he's using jquery mobile, I'm rightfully assuming that jquery is being used for his project, and that a jquery answer (that works) would be fair game. – Todd Mar 27 '13 at 06:14
  • if jquery can't handle an event, css sure as heckfire cannot either. – Todd Mar 27 '13 at 06:15
  • 2
    Yes, I'm saying you need to mention that it isn't (possible with) CSS because the question specifically asked to do it via CSS, not jQuery. – BoltClock Mar 27 '13 at 06:19
  • 3
    This doesn't work (anymore), at least not with the latest jQuery. – hon2a Jul 17 '17 at 08:56
2

In CSS it's not possible, because you can only add contents to DOM (tipically with :before :after and content: '...';, not remove or change attributes.

The only way is to create a live custom event (es. "change-something"):

$("a").on("change-something", function(event) { this.removeAttr("title"); });

and trigger to every changes:

... $("a").trigger("change-something");

More information and demo here:

http://api.jquery.com/trigger/
http://api.jquery.com/removeAttr/

g.annunziata
  • 3,118
  • 1
  • 24
  • 25
2

try to change your code using this

$(document).ready(function() {
    $("a").removeAttr("title");
});

this will remove title attribute so the hint label won't be appear when hover on the link

0

Full working pure javascript solution

var anchors = document.querySelectorAll('a[title]');
    for (let i = anchors.length - 1; i >= 0; i--) {
    anchors[i].addEventListener('mouseenter', function(e){
        anchors[i].setAttribute('data-title', anchors[i].title);
        anchors[i].removeAttribute('title');
        
    });
    anchors[i].addEventListener('mouseleave', function(e){
        anchors[i].title = anchors[i].getAttribute('data-title');
        anchors[i].removeAttribute('data-title');
    });
}
hugsbrugs
  • 3,501
  • 2
  • 29
  • 36
0
  $("#test").tooltip({title: false});

There title attribute default value is true, make it false. This will work only in case of Bootstrap Tooltip

0
const config = {
  attributes: true,
  childList: true,
  subtree: true,
  attributeFilter: ['title'],
};
const removeTitleAttribute = (element, observer) => {
  if (element.hasAttribute('title')) {
    element.removeAttribute('title');
  }
};
const removeTitleByObserveDOM = () => {
  const observer = new MutationObserver(mutations => {
    mutations.forEach(mutation => {
      if (mutation.type === 'childList') {
        mutation.addedNodes.forEach((node: HTMLElement) => {
          if (node.nodeType === Node.ELEMENT_NODE) {
            const components = node.querySelectorAll('[title]');
            components.forEach(component => removeTitleAttribute(component, observer));
          }
        });
      } else if (mutation.type === 'attributes' && mutation.attributeName === 'title') {
        removeTitleAttribute(mutation.target, observer);
      }
    });
  });

  observer.observe(document.querySelector('#root'), config);
  return observer;
};
export default removeTitleByObserveDOM;
Circle
  • 1
  • 1
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Apr 16 '23 at 04:20