Select <a> which href ends with some string

494,205

Solution 1

   $('a[href$="ABC"]')...

Selector documentation can be found at http://docs.jquery.com/Selectors

For attributes:

= is exactly equal
!= is not equal
^= is starts with
$= is ends with
*= is contains
~= is contains word
|= is starts with prefix (i.e., |= "prefix" matches "prefix-...")

Solution 2

$('a[href$="ABC"]:first').attr('title');

This will return the title of the first link that has a URL which ends with "ABC".

Solution 3

$("a[href*='id=ABC']").addClass('active_jquery_menu');

Solution 4

$("a[href*=ABC]").addClass('selected');

Solution 5

Just in case you don't want to import a big library like jQuery to accomplish something this trivial, you can use the built-in method querySelectorAll instead. Almost all selector strings used for jQuery work with DOM methods as well:

const anchors = document.querySelectorAll('a[href$="ABC"]');

Or, if you know that there's only one matching element:

const anchor = document.querySelector('a[href$="ABC"]');

You may generally omit the quotes around the attribute value if the value you're searching for is alphanumeric, eg, here, you could also use

a[href$=ABC]

but quotes are more flexible and generally more reliable.

Share:
494,205
Emiel Bruijntjes
Author by

Emiel Bruijntjes

Updated on June 02, 2020

Comments

  • Emiel Bruijntjes
    Emiel Bruijntjes about 4 years

    Is it possible using jQuery to select all <a> links which href ends with "ABC"?

    For example, if I want to find this link <a href="http://server/page.aspx?id=ABC">

  • gman
    gman almost 13 years
    something changed recently. $('[href$=-abc]') used to work. Now it requires quotes $('[href$="-abc"]') I don't know when it changed. Maybe it was always supposed to require quotes and just happened to work before.
  • sparkyspider
    sparkyspider almost 13 years
    Correction: Which ends with ABC
  • alekwisnia
    alekwisnia over 12 years
    Actually, there is a slight difference. This will select the first link with given href, which is useful if you need to change only one.
  • Louis Somers
    Louis Somers about 12 years
    Note that "ABC" is case-sensitive! (Just spent quite some time to figure that one out...)
  • sscirrus
    sscirrus about 11 years
    For future visitors who may be helped by that answer.
  • sscirrus
    sscirrus about 11 years
    @Sumit note that your answer is only correct if the OP's ABC happens to refer to an ID.
  • sf.dev
    sf.dev about 10 years
    How to get href does not contains ABC in jquery
  • tvanfosson
    tvanfosson about 10 years
    @sf.dev $('a').filter(function() { return !this.href || !this.href.match(/ABC/); });
  • k-nut
    k-nut over 9 years
    This works with vanilla javascirpt now. You can simply use document.querySelectorAll('a[href$="ABC"]') to achieve this.
  • bitten
    bitten over 7 years
    != is not a valid attribute selector
  • tvanfosson
    tvanfosson over 7 years
    @bitten the question is about jQuery attribute selectors. It is a valid conditional operation in jQuery.
  • Arun
    Arun over 5 years
    if someone is trying to use a var var name = "ABC"; $('a[href$='+name+']')