How get element by data-attribute in javasript?
I have this data-attribute
How better interact with this element
console.log(document.querySelector("dropdown[aria-current]"))
returns null
I'm use react - any ideas how do it better with react?
SOLUTION

14 Replies
it's not a dropdown element, you're missing the
.
to make it a class selector
also, even then, I don't see the dropdown class, just .dropdown-item
, so .dropdown-item[aria-current]
should work as a selectorIts was so simple

I got next error - cna you help please?
If I check it like this its also doesn't help
what's the error?
wait, nm, missed the screenshot
Object is possibly 'null'
sorry, forgot a bit before sending
Now it works fine

Thank you
np 🙂 The reason typescript was doing that, is that it doesn't actually understand what querySelector does, beyond take a string and return either null or an HTMLElement. It can't know that running it twice (once in the if, then again just below) will yield the same result, so it defaults back to checking the return signature. The code was valid, and wouldn't have ever produced an error unless there's a really weird situation where something else deletes the element between the
if
and addEventListener
calls@jochemm may I ask you in this thread - what to do if function doesn't work
I mean I got no console.log

ah! I missed that copying your code, the vanilla event is called
mouseleave
, no on
now it works
thank you again!
no worries 😄