0

Does setattribute function in js replaces the existing attributes and its values?

For example: I have class attribute with two values. Will the class attribute be replaced when i use setattribute for class?

4th Dec 2021, 5:53 PM
Pranay Sehgal
Pranay Sehgal - avatar
7 Réponses
+ 1
Pranay Sehgal no it doesn't replace them but It will remove all previous class And set a new value For example : Before <div class="clas claz" ></div> JS Clas. SetAttribute("newClass") After <div class="newClass" ></div>
4th Dec 2021, 6:53 PM
Pariket Thakur
Pariket Thakur - avatar
+ 1
Yes
4th Dec 2021, 6:11 PM
Pariket Thakur
Pariket Thakur - avatar
4th Dec 2021, 6:23 PM
SoloProg
SoloProg - avatar
+ 1
HTMLElement.classList.add (YOUR_CLASS ) for info "Element.classList - Web APIs | MDN" https://developer.mozilla.org/en-US/docs/Web/API/Element/classList
5th Dec 2021, 11:55 AM
Pariket Thakur
Pariket Thakur - avatar
0
It replaces them?
4th Dec 2021, 6:12 PM
Pranay Sehgal
Pranay Sehgal - avatar
0
Beauty kindly reply
4th Dec 2021, 6:13 PM
Pranay Sehgal
Pranay Sehgal - avatar
0
Okay Beauty thanks is there any other method when i can use to add a new class rather than replacing them?
5th Dec 2021, 6:07 AM
Pranay Sehgal
Pranay Sehgal - avatar