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):

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

and with conditional formatting also (second case):

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

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.