dbzx10299
dbzx10299

Reputation: 1084

Check if element attribute has specific value JS

Is there a way in javascript to check if an element has a certain attribute value? Given this code:

<div class="wrapper">
   <a href=#refresh-cart>view cart</a>
</div>

is there a way to be more specific rather than

document.addEventListener('click', e => {
  if (e.target.hasAttribute('href')) {
    //...
  }
})

Upvotes: 1

Views: 555

Answers (1)

Quentin
Quentin

Reputation: 943564

Get the value. Compare it to what you want.

if (e.target.getAttribute('href') === "foo")

Upvotes: 5

Related Questions