1

I am trying to create custom tooltip plugin just for learning purpose. Every thing is working fine when I take tooltip text in a custom attribute like "tooltip", but in case of "title" I am not able to hide / disable browser default title tooltip.

HTML:

<p id="p1" title="tooltip 1">Go</a>">
    This is a paragraph
</p>

<p id="p2" title="tooltip 2">
    This is another paragraph
</p>

JQuery:

$("p").tooltip({
    autoClose: false,
    color: "white   ",
    backgroundColor: "grey",
});

Working Fiddle

1
  • use data-title for tooltip, on jquery you can get varibale from data-title ? Commented Jan 17, 2017 at 11:13

3 Answers 3

2

in your provided JSFiddle, you have following code

var tooltipContent =  $(this).attr('data-ttl');
var newElem = '<span class="tooltip">'+ tooltipContent +'</span>';

instead of adding title, try to add following data attribute in your P Tag

<p id="p1" data-title="tooltip 1">Go</a>">
    This is a paragraph
</p>

<p id="p2" data-title="tooltip 2">
    This is another paragraph
</p>

and change code to following

var tooltipContent =  $(this).data('data-title');
var newElem = '<span class="tooltip">'+ tooltipContent +'</span>';

Please find updated JS Fiddle

EDIT

If you check the actual jQuery UI tooltip, default browser tooltip doesn't appear. jQuery UI Tooltip

The reason is that, if you check HTML in Developer tool window you can see the library itself remove content in "title" attribute on mouseenter and reset it again on mouseout. After looking at your fiddle, it seems you are adding your own logic to show and hide tooltip. Well incase you add your custom logic you have to handle those all cases as well. Do following step to have same behavior as default jQuery tooltip.

Remove content in the "title" attribute when you create tooltip and show (on mouse enter).

Store that content some where or in "data" attribute of tag.

Reset the content again to "title" attribute when user moves out the mouse from the control.

Please refer this JS Fiddle with solution for your issue. i have modified your tooltip click event to restore the title content

Updated JS Fiddle

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

4 Comments

I know I can use custom attribute, but I want to use title. Thanks for your effort.
Check my answer with edited solution. You can achieve it with the proposed step
OK, sorry I forgot to check the DOM of jquery UI tooltip. +1 for your effort
Hello Mayank, please find updated Fiddle in my answer with solution i mentioned before.
0

you can not disable custom browser tooltip (you can do it with javascript not default way). However you can use this trick :

<p id="p1" data-title="tooltip 1">
    This is a paragraph
</p>

on jquery you can get variable from data-title :

var tooltipContent =  $(this).attr('data-title');

1 Comment

I know I can use custom attribute, but I want to use title. Thanks for your effort.
0

Workaround with some js to remove title attribute & add a class for css/future ref.

const elem_title = document.querySelector('#elem_with_title');
elem_title.classList.add('newclassname');
elem_title.removeAttribute('title');

Comments

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.