jQuery: How to get the value of an html attribute?
I've got an html anchor element:
<a title="Some stuff here">Link Text</a>
...and I want to get the contents of the title so I can use it for something else:
$('a').click(function() {
var title = $(this).getTheTitleAttribute();
alert(title);
});
How can I do this?
$('a').click(function() {
var title = $(this).attr('title');
alert(title);
});
$('a').click(function() {
var title = $(this).attr('title');
alert(title);
});
$(this).attr("title")
You can simply use this.title
inside the function
$('a').click(function() {
var myTitle = $(this).attr ( "title" ); // from jQuery object
//var myTitle = this.title; //javascript object
alert(myTitle);
});
Note
Use another variable name instead of 'alert'. Alert is a javascript function and don't use it as a variable name