Saturday, December 11, 2010

Enumerate through all href using jQuery – Update all URLs on page using jQuery

I first tried doing the following:

$.each($('a'), function (index, value) {
                alert($('a')[index]);
            });

Well, the above code works and gives you the absolute URLs, however, you can’t make any change to the anchor tag itself, which is why most likely you are enumerating. Therefore, I rewrote the code as follows:

$('a').each(function () {
   alert($(this).attr('href'));
});

You can clearly see, this is indeed the right way to go!

No comments:

Post a Comment

Feel free to give constructive feedback or let me know if it was helpful.