How to get an element by its href in jquery?

eos87 picture eos87 · Jun 24, 2010 · Viewed 154.7k times · Source

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

Answer

BalusC picture BalusC · Jun 24, 2010

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

var linksToGoogle = $('a[href="http://google.com"]');

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

var allLinksToGoogle = $('a[href^="http://google.com"]');