10

I'm new to Javascript, and wish to change the text of a tooltip with Javascript.

I've had a search around the questions but can't seem to find anything illuminating.

Basically I need to get the tooltip text then replace it with something else, without disturbing the underlying HTML.

Thanks heaps.

6
  • 1
    Can you show use your HTML? Tooltip - do you mean alt and title attributes? Commented Nov 23, 2015 at 21:41
  • javascriptkit.com/script/script2/tooltip.shtml ?? Commented Nov 23, 2015 at 21:45
  • I mean the Title attribute, sorry for the lack of detail. Commented Nov 24, 2015 at 0:06
  • This is what I am trying to access: the title here: Commented Nov 25, 2015 at 23:02
  • <div id="logo"> <a title="google.com" aria-label="Link action for logo" href="google.com" target="_blank" border="0"> <img width="202" height="49" aria-hidden="true" alt="logo" src="0-logo-bd.png" border="0"> </a> </div> Commented Nov 25, 2015 at 23:02

5 Answers 5

13

Assuming that elid is the ID of the element carrying the tooltip:

document.getElementById('elid').setAttribute('title', '_new content_');
Sign up to request clarification or add additional context in comments.

Comments

4

Quick example, I assume you mean the title attribute?

Fiddle: http://jsfiddle.net/AtheistP3ace/wmzLs7gp/1/

HTML:

<a title="some text" id="blah">hi</a>
<button id="mybutton">change tooltip</button>

JS:

document.getElementById('mybutton').addEventListener('click',
    function () {
        // These following two lines are the part that matter
        var anchor = document.getElementById('blah');
        anchor.title = 'new tooltip';
    }
);

Comments

1

I'm unsure what you mean my Tooltip, I'm assuming you mean the alt attribute on an image or the title attribute you get some other HTML tags, with such lack of information I'd recommend looking into the setAttribute method, for example

document.getElementById("myIdReference").setAttribute("alt", "My new Alt text");

There's more info here...

http://www.w3schools.com/jsref/met_element_setattribute.asp https://developer.mozilla.org/en/docs/Web/API/Element/setAttribute

2 Comments

Turns out there were other title elements, so this worked:<script> $(document).ready(function(){ $('#logo a').removeAttr('title'); $('#logo img').removeAttr('title'); $('#logo).attr('title', 'Click here to go to our website'); }); </script> All sorted using JQuery in the end.
Thanks heaps for your input.
1

You can do it without attribute.

document.getElementById("myIdReference").title = 'my tooltip text'

Comments

0

So this is dependant on where you are getting the data from that you wish to replace the tooltip text with. However you can replace the tooltip text as follows:

You will need to reference the tooltip element in javascript. Next you need to set the text that you wish to change.

Basic selectors in javascript look like this

var myElement = document.getElementById('yourElementId');

See the example JSFiddle that will change the text as soon as you run. Hopefully this should provide a basis on how to make the change.

1 Comment

Assuming I have understood exactly what you're looking to achieve :)

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.