+ 1

Why this icon do not change

But if i replace fa-times with something else it's work. example When i replace fa-times with fa-thumbs-up it's work https://code.sololearn.com/WCtP0SWzc18r/?ref=app

17th Sep 2021, 1:27 PM
EsaKurniawan
12 Respuestas
+ 5
You need to toggle both icons Try this: function whatever(burger) { burger.classList.toggle("fa-bars"); burger.classList.toggle("fa-times"); }
17th Sep 2021, 4:29 PM
Calviղ
Calviղ - avatar
+ 2
HTML DOM classList Property https://www.w3schools.com/jsref/prop_element_classlist.asp Keep learning & happy coding :D
17th Sep 2021, 2:14 PM
SoloProg
SoloProg - avatar
+ 2
HrCoder thanks it's can work now
17th Sep 2021, 2:24 PM
EsaKurniawan
+ 2
HrCoder I put the link "classList property" for general use in websites.
18th Sep 2021, 12:57 PM
SoloProg
SoloProg - avatar
+ 1
EsaKurniawan u can try this function whatever(burger) { if (burger.classList.contains('fa-bars')) { burger.classList.remove('fa-bars') burger.classList.add('fa-times'); } else { burger .classList.remove('fa-times') burger.classList.add('fa-bars'); } /* here is the problem */ }
17th Sep 2021, 2:11 PM
Pariket Thakur
Pariket Thakur - avatar
+ 1
SoloProg there is no mistakes on classList property
17th Sep 2021, 2:17 PM
Pariket Thakur
Pariket Thakur - avatar
+ 1
Html <i id="oke" class="fa fa-bars"></i> Js let oke = document.getElementById("oke"); oke.addEventListener('click', function(){ whatever(this); }); function whatever(burger) { burger.classList.toggle("fa-bars"); burger.classList.toggle("fa-times"); }
18th Sep 2021, 2:05 PM
SoloProg
SoloProg - avatar
+ 1
The icon will not change because fa-bars has precedence over fa-times and precedence is the reason it changes when you switch to fa-thumbs-up but in this case, fa-thumbs-up has precedence over fa-bars. Fa Icon Cheatsheet https://fontawesome.com/v4.7/cheatsheet/ This is the case for all of the fa icons, when you add another icon to i elements classList one of the icons will have precedence over the other, and the icon with the highest will be displayed https://code.sololearn.com/Wb5mqKgVlhSA
18th Sep 2021, 2:35 PM
ODLNT
ODLNT - avatar
0
But why when i replace fa-times with fa-thumbs-up it's can work
17th Sep 2021, 1:44 PM
EsaKurniawan
0
thank you for your answer
17th Sep 2021, 2:08 PM
EsaKurniawan
0
Calviղ thanks your answer can work too
18th Sep 2021, 1:03 AM
EsaKurniawan
- 1
EsaKurniawan i think so fa-times is not supporting
17th Sep 2021, 2:05 PM
Pariket Thakur
Pariket Thakur - avatar