How to get an element by its href in jquery?

JavascriptJqueryHtmlJavascript Framework

Javascript Problem Overview


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

Javascript Solutions


Solution 1 - Javascript

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

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

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

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

Solution 2 - Javascript

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

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

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

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

Solution 3 - Javascript

var myElement = $("a[href='http://www.stackoverflow.com']");

http://api.jquery.com/attribute-equals-selector/

Solution 4 - Javascript

Yes.

$('a[href=\'http://google.com\']')

http://api.jquery.com/attribute-equals-selector/

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questioneos87View Question on Stackoverflow
Solution 1 - JavascriptBalusCView Answer on Stackoverflow
Solution 2 - JavascriptjonathancardosoView Answer on Stackoverflow
Solution 3 - JavascriptAdamView Answer on Stackoverflow
Solution 4 - JavascriptMarkView Answer on Stackoverflow