0
<p class="URL-List">
    Download : 
    <a href="https://exampleAA.com"><strong>AA</strong></a> / 
    <a href="https://exampleBB.com"><strong>BB</strong></a> / 
    <a href="https://exampleCC.com"><strong>CC</strong></a>
</p>

My Code

AA = URL[0].getElementsByTagName("a")
AA[0]

I will get the first URL:

https://exampleAA.com

But the list changes

<p class="URL-List">
    Download : 
    <a href="https://exampleCC.com"><strong>CC</strong></a> / 
    <a href="https://exampleDD.com"><strong>DD</strong></a> / 
    <a href="https://exampleSS.com"><strong>SS</strong></a> /
    <a href="https://exampleAA.com"><strong>AA</strong></a> / 
    <a href="https://exampleFF.com"><strong>FF</strong></a> / 
    <a href="https://exampleBB.com"><strong>BB</strong></a>
</p>

My Code

AA = URL[0].getElementsByTagName("a")
AA[0]

Now I get: `

https://exampleCC.com

` But I want

https://exampleAA.com

How can I find that specific URL?

Rayly Esta
  • 33
  • 8
  • unclear how you are supposed to get that URL. What do we have that gives us any idea you need that URL? – epascarello Nov 02 '20 at 21:35
  • What function is calling the Javascript? What event triggers your js? How are you indicating which url to return? – mykaf Nov 02 '20 at 21:37

1 Answers1

2

You can use .querySelector() with attribute selector syntax if you know which link you want:

let myLink = document.querySelector("a[href='https://exampleAA.com']");

console.log(myLink.textContent, myLink.href);
<p class="URL-List">
    Download : 
    <a href="https://exampleCC.com"><strong>CC</strong></a> / 
    <a href="https://exampleDD.com"><strong>DD</strong></a> / 
    <a href="https://exampleSS.com"><strong>SS</strong></a> /
    <a href="https://exampleAA.com"><strong>AA</strong></a> / 
    <a href="https://exampleFF.com"><strong>FF</strong></a> / 
    <a href="https://exampleBB.com"><strong>BB</strong></a>
</p>

Do not use .getElementsByTagName() in 2020.

Scott Marcus
  • 64,069
  • 6
  • 49
  • 71