How to change an element's title attribute using jQuery

453,744

Solution 1

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.

Solution 2

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

$( "#my_dialog" ).dialog( "option", "title", "my new title" );

Solution 3

I beleive

$("#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.

Solution 4

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.

Solution 5

jqueryTitle({
    title: 'New Title'
});

for first title:

jqueryTitle('destroy');

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

Share:
453,744

Related videos on Youtube

Brian
Author by

Brian

Updated on June 05, 2020

Comments

  • Brian
    Brian almost 4 years

    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?

  • Ryan Ellis
    Ryan Ellis over 10 years
    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!
  • VoidKing
    VoidKing over 10 years
    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!)
  • Cᴏʀʏ
    Cᴏʀʏ over 10 years
    @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!
  • VoidKing
    VoidKing over 10 years
    @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!
  • Cᴏʀʏ
    Cᴏʀʏ over 10 years
    @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.
  • VoidKing
    VoidKing over 10 years
    @Cory Thank you, Sir!
  • Infant Dev
    Infant Dev over 10 years
    @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>
  • Cᴏʀʏ
    Cᴏʀʏ over 10 years
    @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.
  • Michel
    Michel almost 10 years
    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.
  • Igor L.
    Igor L. about 9 years
    Never would have thought this would be accepted that many times. I am very glad that it helped :-)
  • Avishek
    Avishek about 9 years
    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.
  • Erdi Ertaş
    Erdi Ertaş about 9 years
    Yes, but there are other features. Simple but nice solution... You can examine...
  • Avishek
    Avishek about 9 years
    there would be an http transport overhead of the 3rd-party library, which can be avoided using stock/native methods.
  • Avishek
    Avishek about 9 years
    plus, there is already a broadly accepted answer which is more efficient.
  • Countzero
    Countzero over 3 years
    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.