JavaScript
Article

jQuery extract hyperlinks with specific href

By Sam Deering

Simple jQuery code snippet to use jQuery selector to get all those href’s based on a wildcard search. I was just writing a jQuery selector to extract hyperlinks based on wildcard matches and thought I would share it on the blog.

//output all links on page that start with "http://www.jquery4u.com"
$('#j4u-post a[href^="http://www.jquery4u.com"]').each(function (i,v)
{
    console.log($(this).attr('href'));
});

extract-links

Now to add a wildcard into our selector we can do this:

$('#j4u-post a[href*="jquery4u.com"]').each(function (i,v)
{
    console.log($(this).attr('href'));
});

Using the *= essentially means “like” and the ^= means starts with. Thats it.

Free Guide:

7 Habits of Successful CTOs

"What makes a great CTO?" Engineering skills? Business savvy? An innate tendency to channel a mythical creature (ahem, unicorn)? All of the above? Discover the top traits of the most successful CTOs in this free guide.

No Reader comments

Recommended
Sponsors
Because We Like You
Free Ebooks!

Grab SitePoint's top 10 web dev and design ebooks, completely free!

Get the latest in JavaScript, once a week, for free.