0

So I'm creating a page where i need some sort of class add on click of an element. My problem is className and classList both don't work in older IOS safari clients. Does anyone know an alternative to:

var answer = target.parentElement.querySelector('.icon');
answer.className = "answer-text";

or

var answer = target.parentElement.querySelector('.icon');
answer.classList.add('Active');

I can't seem to find any alternatives on the web.

Sybrentjuh
  • 247
  • 3
  • 14
  • Does this answer your question? [How do I add a class to a given element?](https://stackoverflow.com/questions/507138/how-do-i-add-a-class-to-a-given-element) – VLAZ Oct 23 '20 at 10:07
  • According to [caniuse](https://caniuse.com/mdn-api_element_classlist_add_and_remove_multiple_arguments) `.classList.add(x)` should work on iOS Safari 7 and up. Do you have to support versions lower than that? – VLAZ Oct 23 '20 at 10:10
  • see this https://stackoverflow.com/questions/17769563/using-getelementbyclassname-in-safari – Fahad Subzwari Oct 23 '20 at 10:11
  • @FahadSubzwari that's for `getElementsByClassName` not adding classes. – VLAZ Oct 23 '20 at 10:15
  • @VLAZ no unfortunately not and yes, the problem here is it needs to support versions lower than Safari 7. It would have been no problem otherwise :( – Sybrentjuh Oct 23 '20 at 10:23

1 Answers1

0

According to MDN

The property attributes.class is supported in Safari. So you can write

answer.attributes.class.value = "answer-text";
mylee
  • 1,293
  • 1
  • 9
  • 14