How to change an element's title attribute using jQuery
Asked Answered
R

7

270

I have an form input element and want to change its title attribute. This has to be easy as pie, but for some reason I cannot find how to do this. How is this done, and where and how should I be searching on how to do this?

Roue answered 12/6, 2009 at 17:32 Comment(0)
S
538

Before we write any code, let's discuss the difference between attributes and properties. Attributes are the settings you apply to elements in your HTML markup; the browser then parses the markup and creates DOM objects of various types that contain properties initialized with the values of the attributes. On DOM objects, such as a simple HTMLElement, you almost always want to be working with its properties, not its attributes collection.

The current best practice is to avoid working with attributes unless they are custom or there is no equivalent property to supplement it. Since title does indeed exist as a read/write property on many HTMLElements, we should take advantage of it.

You can read more about the difference between attributes and properties here or here.

With this in mind, let's manipulate that title...

Get or Set an element's title property without jQuery

Since title is a public property, you can set it on any DOM element that supports it with plain JavaScript:

document.getElementById('yourElementId').title = 'your new title';

Retrieval is almost identical; nothing special here:

var elementTitle = document.getElementById('yourElementId').title;

This will be the fastest way of changing the title if you're an optimization nut, but since you wanted jQuery involved:

Get or Set an element's title property with jQuery (v1.6+)

jQuery introduced a new method in v1.6 to get and set properties. To set the title property on an element, use:

$('#yourElementId').prop('title', 'your new title');

If you'd like to retrieve the title, omit the second parameter and capture the return value:

var elementTitle = $('#yourElementId').prop('title');

Check out the prop() API documentation for jQuery.

If you really don't want to use properties, or you're using a version of jQuery prior to v1.6, then you should read on:

Get or Set an element's title attribute with jQuery (versions <1.6)

You can change the title attribute with the following code:

$('#yourElementId').attr('title', 'your new title');

Or retrieve it with:

var elementTitle = $('#yourElementId').attr('title');

Check out the attr() API documentation for jQuery.

Sluggard answered 12/6, 2009 at 17:33 Comment(6)
If this isn't a well constructed answer, none are. AND it is as accurate as it is well constructed. +1... (oh, and also your name is awesome, because it is mine :) even spelled the same!)Frazee
@VoidKing: I tend to revisit and make current answers that become popular (of which I only have a few). I try to provide everything that I would hope to find if I were researching or posting a question. I'm glad you found it!Kulak
@Cory Well, to be honest as I was looking for something else (it was a long shot, but was looking if there was any way to style the default HTML title box). I didn't find what I was looking for, but really admired the effort you put into this answer (you showed resources, pure .js and jQuery, etc.). Anyway, nice answer!Frazee
@VoidKing: This is probably the wrong place to answer, but if you're talking about the default tooltips that appear when you hover over items with alt or title attributes, then perhaps this answer will give you some insight.Kulak
@Cory What if I want to set the title attribute with not just a string but as a div? For eg: $("#ele").title = ${"#menu") where <div id="menu"> <a href="www.website.com">Link></a></div>Boggers
@infantDev: You can set the title property to any string. However, browsers don't support rendering HTML in titles (which, when hovered, display as tooltips). You have to take it one step farther, which would be to find a library that will render a custom tooltip based on the title attribute, such as the jQuery UI.Kulak
L
33

In jquery ui modal dialogs you need to use this construct:

$( "#my_dialog" ).dialog( "option", "title", "my new title" );
Logger answered 19/7, 2013 at 12:12 Comment(5)
Lifesaver!! When using the voted answer above, the title is changed once, then the title does not change thereafter UNLESS you use the title option within the dialog!! THANKS SOOOOOOO MUCH!Polik
I'm glad I read the comments ;-) If you need to change the title several time, you have to set it in the dialog options.Recover
Never would have thought this would be accepted that many times. I am very glad that it helped :-)Logger
Thanks a lot for this. Just wanted to add you have to initialize the dialog the usual way first, then use this line of code. Wasn't clear to me, so hope it will help others.Sekofski
Yep, this is the situation I had also, and the other solutions did not work.Hectic
K
21

I believe

$("#myElement").attr("title", "new title value")

or

$("#myElement").prop("title", "new title value")

should do the trick...

I think you can find all the core functions in the jQuery Docs, although I hate the formatting.

Kathie answered 12/6, 2009 at 17:34 Comment(0)
H
7

Another option, if you prefer, would be to get the DOM element from the jQuery object and use standard DOM accessors on it:

$("#myElement")[0].title = "new title value";

The "jQuery way", as mentioned by others, is to use the attr() method. See the API documentation for attr() here.

Homeopathic answered 12/6, 2009 at 17:44 Comment(0)
Y
2

If you are creating a div and trying to add a title to it.

Try

var myDiv= document.createElement("div");
myDiv.setAttribute('title','mytitle');
Ywis answered 18/11, 2015 at 7:29 Comment(0)
G
0
jqueryTitle({
    title: 'New Title'
});

for first title:

jqueryTitle('destroy');

https://github.com/ertaserdi/jQuery-Title

Goddamn answered 16/4, 2015 at 12:58 Comment(4)
This problem can be solved by using the jquery library itself. You should only refer to using 3rd-party libraries when the OP mentions it, or it can't be accomplished without using a 3rd-party library.Theurgy
Yes, but there are other features. Simple but nice solution... You can examine...Zodiac
there would be an http transport overhead of the 3rd-party library, which can be avoided using stock/native methods.Theurgy
plus, there is already a broadly accepted answer which is more efficient.Theurgy
C
0

As an addition to @Cᴏʀʏ answer, make sure the title of the tooltip has not already been set manually in the HTML element. In my case, the span class for the tooltip already had a fixed tittle text, because of this my JQuery function $('[data-toggle="tooltip"]').prop('title', 'your new title'); did not work.

When I removed the title attribute in the HTML span class, the jQuery was working.

So:

<span class="showTooltip" data-target="#showTooltip" data-id="showTooltip">
      <span id="MyTooltip" class="fas fa-info-circle" data-toggle="tooltip" data-placement="top" title="this is my pre-set title text"></span>
</span>

Should becode:

<span class="showTooltip" data-target="#showTooltip" data-id="showTooltip">
      <span id="MyTooltip" class="fas fa-info-circle" data-toggle="tooltip" data-placement="top"></span>
</span>
Correspondence answered 4/6, 2020 at 13:28 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.