Get an element by href using JavaScript

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

HTML
<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:

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

console.log(element);
// <a href="/urlB"></a>

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