Languages
[Edit]
EN

JavaScript - style HTML elements with custom attribute only

0 points
Created by:
Anisha-Kidd
652

In this article, we would like to show you how to style HTML elements containing custom attribute only using JavaScript.

Quick solution:

 

Practical example

Edit

In this example, we use hasAttribute() method to check if the elements with class1 also contain myAttribute, then we style only the ones that do have the attribute.

Style elements with a specific attribute value

Edit

References

Edit
  1. Document.querySelectorAll() - Web APIs | MDN
  2. Element.hasAttribute() - Web APIs | MDN
  3. Element.getAttribute() - Web APIs | MDN

Alternative titles

  1. JavaScript - style HTML elements with data-attribute only
1
Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.
Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join