I looked at this thread but it didn't help: Find an element in DOM based on an attribute value
I need a Vanilla JS solution to select a username that is in a custom attribute of an <a>
tag in the DOM. I used const users = document.querySelectorAll('a[metadata]')
which returns a NodeList of all the <a>
tags with all their attributes. If I type console.log(users[0])
, I get:
<a href="#" class="btn-normal" metadata="
{
'username':'johnny134',
'category':'shoes',
'summary':'Just a guy called Johnny',
'userId':1223432432
}"
</a>
To access the username, I have tried const usernames = document.querySelectorAll('a[metadata="username"]');
but just get back undefined. I think my issue is that username is in an object and I can't figure out how to select it.
Thanks in advance