javascript – How to get an element by its href in jquery?-ThrowExceptions

Exception or error:

I want to get an element by its href attribute in jquery or javascript. Is that possible?

How to solve:

Yes, you can use jQuery’s attribute selector for that.

var linksToGoogle = $('a[href=""]');

Alternatively, if your interest is rather links starting with a certain URL, use the attribute-starts-with selector:

var allLinksToGoogle = $('a[href^=""]');


If you want to get any element that has part of a URL in their href attribute you could use:

$( 'a[href*=""]' );

This will select all elements with a href that contains, for example:

As stated by @BalusC in the comments below, it will also match elements that have at any position in the href, like


var myElement = $("a[href='']");




Leave a Reply

Your email address will not be published. Required fields are marked *