Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I select all anchors with a specific href?

Tags:

html

jquery

I've got the following code to track pageviews for external links matching a particular URL.

    $("a").each(function(i){
        if (
            $(this).attr('href') == "http://example.com/external/link/" || 
            $(this).attr('href') == "http://example.com/external/link"
        ) {
            $(this).click(function(){
                _gaq.push(['_trackPageview', '/external/pagename']);
            });
        }
    });

This code works, but it's extremely inefficient for pages with a lot of links. Is there a way to use a selector to select all the anchors with matching hrefs instead of scanning through all the links on the page?

like image 238
WNRosenberg Avatar asked Jan 07 '11 20:01

WNRosenberg


People also ask

How do I select an element using href?

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: http://google.com.

How do I find all the anchor elements on a website?

To find the anchor elements in a particular web page, we need to open the source of web page by using the browser. After that, you can click ctrl+u. Then, you can copy the source code in the text and also click ctrl+h. It is a simple way to find the anchor text.

Which is the correct JQuery selector to select selected elements having href attribute value equals?

jQuery [attribute$=value] Selector The [attribute$=value] selector selects each element with a specific attribute, with a value ending in a specific string.

Which JQuery statement selects all anchor tags within paragraphs on a page?

getElementsByTagName('a'), for example, selects all anchor tags (links) on a page); and some browsers support methods for selecting all elements with a particular class or using a CSS selector to select page elements.


7 Answers

You can use the Attribute Starts With Selector

$('a[href^="http://example.com/external/link"]').click(function() {
      _gaq.push(['_trackPageview', '/external/pagename']);
});
like image 81
Wookai Avatar answered Oct 26 '22 23:10

Wookai


Yes

$('a[href^="http://example.com/external/link"]').click(function() {});

Using the attribute selector you can look for a particular href. Instead of the normal href= you might expect, I have used href^= which matches any element which starts with the specified string.

Also you do not need to use each to bind to the click event of all the anchor tags you will select. click() will automatically do this for you.

$("a[href^="http://example.com/external/link"]").click(function(){
    _gaq.push(['_trackPageview', '/external/pagename']);
});
like image 42
Marcus Whybrow Avatar answered Oct 27 '22 00:10

Marcus Whybrow


In jQuery getting all hrefs would be like:

var href = 'http://www.google.com';
var elements = $('a[href=' + href + ']');

alert("Found: " + elements.length);
like image 42
Kees C. Bakker Avatar answered Oct 27 '22 00:10

Kees C. Bakker


You can use attribute ends with selector as well if you need to get elements with some specific ending attribute. Something like this:

$('a[href$=your text]') == "your text"

Hope this helps someone.

like image 37
Muhammad Tanweer Avatar answered Oct 27 '22 01:10

Muhammad Tanweer


Another Simple way is:

$('a[href="MY_ANCHOR_TEXT_HERE"]');

Gives the list of all the anchor tags in the current page. To get the anchor tags in a div ID you can use:

$('#MY_DIV_ID a[href="MY_ANCHOR_TEXT_HERE"]')

To get the size use:

$('#MY_DIV_ID a[href="MY_ANCHOR_TEXT_HERE"]').size()
like image 43
man.2067067 Avatar answered Oct 27 '22 01:10

man.2067067


$('a[href^="http://example.com/external/link"]').click( function(e){
  // you do want to track which link was clicked, yes?
  _gaq.push(['_trackPageview', $(this).attr('href') ]);
  // suppress default click or you'll leave the page immediately:
  e.preventDefault(); 
  do_other_stuff_presumably_with_gaq();
});
like image 43
Ken Redler Avatar answered Oct 26 '22 23:10

Ken Redler


Here you can the code:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
      <script>
      $(document).ready(function(){
        $("a").each(function(){
    if($(this).attr("href") == "http://example.com/external/link/")
    {
        $(this).hide();
    }
});
});
 </script>


  <a href="http://example.com/external/link/">a website link</a>
like image 30
Therichpost Avatar answered Oct 26 '22 23:10

Therichpost