Showing posts with label loop through all hyperlink elements. Show all posts
Showing posts with label loop through all hyperlink elements. Show all posts

Thursday, November 3, 2016

loop through all hyperlink elements using jQuery

If you ever wanted to loop through all the hyperlink elements in a page, jQuery provides .each() function which will provide you the desired result.

e.g
$("a").each(function(){
    //access the element here.
});

This can also be done in a more cleaner way as displayed below.

$("a").attr("href", function(i, hRef) {
  //access the element here.
});

I prefer the second option as will not create additional object when iterating elements.

Cheers,
Samitha