Articles

jQuery Select Title Of Any Element Examples

Wondering how to select title of a html element using jQuery? In this article, I am going to share a very simple way to get the title of any html element, be it a div, hyperlink, image, or any other html element.

How to Select Title of any element using jQuery – Examples

Example 1: Select Title of a DIV using jQuery

Let’s assume that we have a div with ID “my_div”, it has some content and we want to get it’s title. Here’s how you can do it:

<script type="text/javascript">

$(document).ready(function() {

var title = $("#my_div").attr('title');

alert(title);

});

</script>

The code above simply grabs the title of the “my_div” div when the page loads and alerts it out. You can use the same procedure to get title of images, hyperlinks, etc. Examples below:

Example 2: Select Title of an image using jQuery

//HTML

<img src="somesrc" id="my_image" width="100" height="100" />

//jQuery code

var img_title = $("#my_image").attr('title');

alert(img_title);

Example 3: Select Title of a hyperlink using jQuery

//HTML

<a href="#" id="my_link">Click here to navigate</a>

//jQuery code

var hyperlink_title = $("#my_link").attr('title');

alert(hyperlink_title);
Very simple, isn’t it?

Do you know of any other ways to select title of html elements using jQuery? Feel free to suggest by commenting below.

Share your thoughts, comment below now!

*

*