0

I want to style differently links that are have the anchor same as the href attribute value:

<a href="https://google.com">https://google.com</a>

Is it possible to do this with CSS alone, or do I need JavaScript for this?

Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
Flavio Copes
  • 4,131
  • 4
  • 27
  • 30

2 Answers2

1

Thanks for your comments on this being impossible in CSS.

I ended up with this JavaScript solution:

document.addEventListener("DOMContentLoaded", () => {
  for (item of document.querySelectorAll("a[href*='//']")) {
    if (item.getAttribute('href') == item.innerHTML) { 
      //item is an HTML link with anchor same as innerHTML.
      //in this case, I just add a class to it

      item.classList.add('no-after-content') 
    }
  }
})
Flavio Copes
  • 4,131
  • 4
  • 27
  • 30
0

A simple solution with javascript . Hope this helps.

<a href="https://google.com">https://google.com/</a><br />
<a href="https://google.com">https://yahoo.com/</a>

<script>
var myCol = document.getElementsByTagName("a");
for(i=0; i<  myCol.length; i++){

if((myCol[i].href).trim() == (myCol[i].innerHTML).trim()){
  myCol[i].style.color = "red";
}

}

</script>
Friday Ameh
  • 1,664
  • 1
  • 10
  • 15