3

I have a button with a PLUS svg image in it. On click, I what that Plus svg to disappear. I checked in console and the function works fine, the class "visible" is removed and the class "invisible" is added. But in the UI the Plus svg doesn't disappear. "Invisible" is a class in Tailwind that should make an item to be hidden.

const BtnAddEle = document.querySelector(".addEle");
const plusSvg = document.querySelector(".addElePlus");

BtnAddEle.addEventListener("click", function () {
  plusSvg.classList.remove("visible");
  plusSvg.classList.add("invisible");
});
Yamyyuky
  • 31
  • 1
  • 3

2 Answers2

2

This is happening because tailwind only adds class styles which you have used in the final CSS file

So you can do something like this

const BtnAddEle = document.querySelector(".addEle");
const plusSvg = document.querySelector(".addElePlus");

BtnAddEle.addEventListener("click", function () {
  plusSvg.style.visibility = "hidden"
});
Heet Vakharia
  • 405
  • 4
  • 14
2

The reason why it is not working for you is that-

To have tailwind classes to work from JS file you have to define the path of js file in the content portion of tailwind.config.js file. Example:

content: ["./*.{html,js}", "./src/**/*.{html,js}"]

Using this, your tailwind compiler will know that you are trying to add a css class from your JS file and that class will appear in the output css file.

Content Configuration is mentioned and explained in details - Here [Official Documentation]