javascriptjqueryhtmljavascript-framework

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?


Solution

  • 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"]');