Anki Forums

How to conditionally add a class to body?

Hi, I’m new to javascript and I’m trying to add a class to the body tag without success.
There are two different request:

  1. adding front and back class to the two faces of the card
  2. adding an anki tag as a class

I tryied (first case, on the front template):

 isOnFront=true;  //false on the back template
 if (isOnFront) {document.body.classList.add("front"); }

and with conditional formatting also (second case):

<span>(Hiragana)</span>      //this is a quick test just to see if it works
	document.body.classList.add("{{Tags}}")  //automatic, or a specific class like Huragana

where, obviously, Hiragana is a sub tag of Japanese.
The script are from the web, other have been tested too. I intsalled the inspector plug in and the class are not added.

Any help?
Thank you

conditionals only check if the field is emtpy or not.

{{#Tags}} some text {{/Tags}}

You cannot use it for checking and match with its content. You can do it via javascript and I suggest the Document.getElementById() way to do it.

Also I suggest to create a div tag instead of body. You can customise the CSS of the div tag to have no margins so it is displayed as the whole body.

do you mean that {{#Tags}}…{{/Tags}} only checks if there is any tag? Can’t I use to check a specific tag?
Thank you

You can only check if a field is empty or non-empty.