jQuery

jQuery Get Title of Div, Image or any element when button is clicked

If you ever wondered how to get / find title of Div, Image or any element when button is clicked, using jQuery, then worry no more. In this article, I am going to show with examples, how easy it is to get the title of an image, a div or any other element using jQuery. Read on to find out more.

jQuery Get Title of Div, Image or any element when button is clicked

Let’s assume that we have a div with ID “my_div” and we want to find the title of this div and alert it when a button is clicked. Here is how we use jQuery to get div title.

Example: Full source code to get image title, div title or title of any element when button is clicked using jQuery

<html>
<head>
<title>jQuery Get Title of Div, Image, any element when button is clicked</title>
<script type="text/javascript" src="js/jquery-1.8.2.min.js"></script>
</head>

<body>

<div id="my_div" title="My DIV title">This is my existing div.</div>

<input type="submit" name="sbt_get_title" id="sbt_get_title" value="Get Div Title">

<script type="text/javascript">

$('#sbt_get_title').on("click", function(){

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

alert( 'Div Title is: '+ title );

});

</script>
</body>
</html>

Run the code, click on the button to instantly alert the div title. You can simply change the selector in the jQuery code and replace it with an image’s ID or class to alert it’s corresponding title. You can apply the same procedure to find title of a div, image or any other element.

Easy?

Do you know of any other ways to find / get Title of Div, Image or any element when button is clicked? Feel free to suggest by commenting below.

Share your thoughts, comment below now!

*

*