Get an element by href using JavaScript

Let's assume that you have created the following list of links:

<a href = '/urlA'></a>
<a href = '/urlB'></a>
<a href = '/urlC'></a>

As an example, if you want to get the second element using a selector:

let element = document.querySelector(`a[href='/urlB']`);

// <a href="/urlB"></a>

Hi, I'm Erik, an engineer from Barcelona. If you like the post or have any comments, say hi.