1

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')) {
    //...
  }
})

1 Answer 1

5

Get the value. Compare it to what you want.

if (e.target.getAttribute('href') === "foo")
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.