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.

No Reader comments

Recommended

Learn Coding Online
Learn Web Development

Start learning web development and design for free with SitePoint Premium!

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