Vanilla JavaScript - Syntax for adding class to element with specific data attr

Le Moi

Trying to add a class to an element with a specific data attribute.

The element is: <a data-type="foo">Linkypoo</a>

I am trying to target like so:

document.querySelectorAll('[data-type="foo"]').classList.add('Booya');

but I am getting the error:

Uncaught TypeError: Cannot read property 'add' of undefined

Could someone point me in the right direction. Not sure how to do this with vanilla js.

Thank you :)

CoderPi

querySelectorAll returns an Element-Collection, to access the first element use querySelectorAll(...)[0]

If you want to apply it to all Elements you can loop through them like in the Example below:

var elements = document.querySelectorAll('[data-type="foo"]')

for (var i = 0; i < elements.length; ++i) {
  elements[i].classList.add('Booya');
}
.Booya {
  color: red;
}
<a data-type="foo">Linkypoo 1</a>
<a data-type="foo">Linkypoo 2</a>
<a data-type="foo">Linkypoo 3</a>
<a data-type="foo">Linkypoo 4</a>

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Adding a class to an element with a specific class

From Dev

Javascript : finding a specific previous element on list and adding class

From Dev

Vanilla Javascript - add Class-name to an element

From Dev

Adding class to an element javascript problemm

From Dev

Adding an event listener to an element that doesn't exist yet in vanilla javascript

From Dev

Javascript Adding Element to DOM Syntax Troubles

From Dev

adding specific hover for each element in class

From Dev

Vanilla javascript: Get a specific element of page in an ajax get request

From Dev

Adding class using attr() in a variable?

From Dev

Adding class to element with matching custom data attribute

From Dev

Javascript - Adding class to element from array

From Dev

Javascript adding styling to element with same class

From Dev

Dynamically add specific attr to element

From Dev

JavaScript Class Syntax: Static Data Members

From Dev

Javascript how to append an element to div with specific class

From Dev

Adding attribute to HTML element causing attr is not a function

From Dev

Adding attribute to HTML element causing attr is not a function

From Dev

vanilla javascript: element.close() not working on element

From Dev

Clone an element with an specific attribute with Vanilla JS

From Dev

select specific element using jQuery attr() Method

From Dev

adding a class attrib to html element based on certain condition javascript

From Dev

Javascript, toggle-button adding style to element by class

From Dev

How to check if an element with .Attr has a particular class?

From Dev

Syntax for jquery data element

From Dev

Vanilla Javascript find element with attribute on click

From Dev

Adding an element to a specific location in a list

From Dev

Adding an element to a specific location in a list

From Dev

Is it possible to scrape data from html element with specific class

From Dev

JQuery: How to select specific element of a class by data-, then use as target?

Related Related

  1. 1

    Adding a class to an element with a specific class

  2. 2

    Javascript : finding a specific previous element on list and adding class

  3. 3

    Vanilla Javascript - add Class-name to an element

  4. 4

    Adding class to an element javascript problemm

  5. 5

    Adding an event listener to an element that doesn't exist yet in vanilla javascript

  6. 6

    Javascript Adding Element to DOM Syntax Troubles

  7. 7

    adding specific hover for each element in class

  8. 8

    Vanilla javascript: Get a specific element of page in an ajax get request

  9. 9

    Adding class using attr() in a variable?

  10. 10

    Adding class to element with matching custom data attribute

  11. 11

    Javascript - Adding class to element from array

  12. 12

    Javascript adding styling to element with same class

  13. 13

    Dynamically add specific attr to element

  14. 14

    JavaScript Class Syntax: Static Data Members

  15. 15

    Javascript how to append an element to div with specific class

  16. 16

    Adding attribute to HTML element causing attr is not a function

  17. 17

    Adding attribute to HTML element causing attr is not a function

  18. 18

    vanilla javascript: element.close() not working on element

  19. 19

    Clone an element with an specific attribute with Vanilla JS

  20. 20

    select specific element using jQuery attr() Method

  21. 21

    adding a class attrib to html element based on certain condition javascript

  22. 22

    Javascript, toggle-button adding style to element by class

  23. 23

    How to check if an element with .Attr has a particular class?

  24. 24

    Syntax for jquery data element

  25. 25

    Vanilla Javascript find element with attribute on click

  26. 26

    Adding an element to a specific location in a list

  27. 27

    Adding an element to a specific location in a list

  28. 28

    Is it possible to scrape data from html element with specific class

  29. 29

    JQuery: How to select specific element of a class by data-, then use as target?

HotTag

Archive