JavaScript
Article
By Sam Deering

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.

--ADVERTISEMENT--

Login or Create Account to Comment
Login Create Account
Recommended
Sponsors
Get the most important and interesting stories in tech. Straight to your inbox, daily.