Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to add a title in anchor tag using jQuery?
To add a title in anchor tag in jQuery, use the prop() method. The prop() method is used to set properties and values of the selected elements.
The title attribute provides additional information about an element and is typically displayed as a tooltip when users hover over the anchor tag. This is particularly useful for improving accessibility and user experience.
You can try to run the following code to learn how to add a title in anchor tag using jQuery ?
Example
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
$("#button1").click(function(){
$('.myClass').prop('title', 'This is your new title');
var myTitle = $('.myClass').prop('title');
alert(myTitle);
});
});
</script>
</head>
<body>
<div id='div1' class="myclass">
<a class="myClass" href='#'><img src='http://tutorialspoint.com/green/images/logo.png' /></a><br>
<button id="button1">Add Title</button>
</div>
</body>
</html>
In this example, when you click the "Add Title" button, the jQuery code will add a title attribute to the anchor tag with the class "myClass". The script then retrieves the newly set title and displays it in an alert box to confirm the operation was successful.
Conclusion
The prop() method in jQuery provides a simple and effective way to dynamically add or modify title attributes in anchor tags. This technique enhances user experience by providing helpful tooltips and improves web accessibility.
