Kosmo零
Kosmo零

Reputation: 4151

How to determine with JavaScript that some element has certain empty attribute declared?

<div aria-selected="" class="variantButton">

When attribute is non empty I do this:

if (elem.getAttribute("aria-selected"))
    //do something

But this will not work in case of empty string.

P.S. I don't use jQuery.

Upvotes: 0

Views: 353

Answers (2)

JerMah
JerMah

Reputation: 763

Use hasAttribute:

console.log( document.getElementsByTagName("div")[0].hasAttribute("aria-selected") )
<div aria-selected="" class="variantButton">

Upvotes: 2

uyghurbeg
uyghurbeg

Reputation: 310

try this:

if (elem.getAttribute("aria-selected") && elem.getAttribute("aria-selected") !== "")

Upvotes: 0

Related Questions