-2

I want to find anchor tags with same hrefs on the current page

For example I have three anchor tags of same href on the current page like

<a href="https://www.link.com" class="btn btn-cta">

The class will be same for all the buttons.

Sorry i forgot to update my code which is updated

  $("a").each(function(){
               if ($(this).attr("href") == window.location.pathname){

               }
       });

Is there any better way of doing this.

Any help or suggestion would be appreciated

Thanks in advance

Owais Ahmed
  • 1,364
  • 1
  • 30
  • 62

1 Answers1

1

Try like this. find your elements using $("[href='"+window.location.pathname+"']") this method.

var path = window.location.pathname;

$("a[href='"+path+"']").each(function(){
  //do your code here
  console.log(window.location.pathname);
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<a href="https://www.link.com" class="btn btn-cta">link1</a>
<a href="/js" class="btn btn-cta">link2</a>
<a href="/js" class="btn btn-cta">link3</a>
<a href="https://www.link.com" class="btn btn-cta">link4</a>
Syed mohamed aladeen
  • 6,507
  • 4
  • 32
  • 59