Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery: Get title and href values as variables

I have a list of links with a title and a href value. I would like to be able to get these values seperately, but I always get the first link's values. Why is that? See my fiddle here. As you can see - when clicking a any of the link, you always get the values from the first link. I guess setting these variables isn't sufficient:

var title = $('.mg_phones').attr('title');
var url = $('.mg_phones').attr('href');

Any ideas?

like image 509
Meek Avatar asked Dec 05 '22 12:12

Meek


2 Answers

You have to refer to the clicked element:

var title = $(this).attr('title');
var url = $(this).attr('href');
like image 162
johankj Avatar answered Dec 26 '22 01:12

johankj


You need to change the code to this:

var title = $(this).attr('title').toLowerCase();
var url = $(this).attr('href');
like image 34
hjpotter92 Avatar answered Dec 25 '22 23:12

hjpotter92