Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get an element by its href in jquery?

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

like image 632
eos87 Avatar asked Jun 23 '10 22:06

eos87


People also ask

What method can be used to retrieve the href attribute of an element?

Use getAttribute() to Get Href in JavaScript The Element interface's getAttribute() method returns the value of a specified attribute for the element.

How add href attribute in jQuery?

Answer: Use the jQuery . attr() Method You can use the jQuery . attr() method to dynamically set or change the value of href attribute of a link or anchor tag. This method can also be used to get the value of any attribute.

What is $() in jQuery?

$() = window. jQuery() $()/jQuery() is a selector function that selects DOM elements. Most of the time you will need to start with $() function. It is advisable to use jQuery after DOM is loaded fully.


1 Answers

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"]'); 
like image 156
BalusC Avatar answered Sep 28 '22 10:09

BalusC