I want to get an element by its href attribute in jquery or javascript. Is that possible?
Use getAttribute() to Get Href in JavaScript The Element interface's getAttribute() method returns the value of a specified attribute for the element.
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.
$() = 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.
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"]');
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With