1

var href = $('a[target="_blank"]').attr('href'); $('a[target="_blank"]').attr('title', 'Öppnas i en ny flik; ' + href);

Let's say I have a link in the header.php file with target="_blank". In the donate.php file I have another link with target="_blank". The code I show you above does only take the href value from the link in the header.php file. How do I take the href value in every link I have on the current page?

If I have the mouse cursor over the link in header.php, it will show "Öppnas i en ny flik; http://the-link.nu/". If I have the mouse cursor over the link in donate.php, it will show "Öppnas i en ny flik; http://another-link.nu/".

Any help to fix this problem? :) Thanks in advance!

Airikr
  • 6,258
  • 15
  • 59
  • 110

2 Answers2

1

use jquery each method:

$('a[target="_blank"]').each(
    function() {
        var href = $(this).attr('href');
        $(this).attr('title', 'Öppnas i en ny flik; ' + href);
    }
);    
mutex
  • 7,536
  • 8
  • 45
  • 66
0
$('a[target="_blank"]').each(function() {
    $(this).attr('title', 'Öppnas i en ny flik; ' + $(this).attr('href'));
});

You're only getting the first anchor's attribute because $.attr() is designed to get just the value from the first matched element. Using $.each lets you loop through all of the elements found.

J Bryan Price
  • 1,364
  • 12
  • 17
  • Additionally, if you're loading your **donate.php** file dynamically, check out http://stackoverflow.com/questions/2548479/jquery-live-on-load-event to update titles as they come in. – J Bryan Price Jul 13 '11 at 00:49