oTree Forum >

Make some words in a form field's label bold

#1 by PimPim

Dear all,

I want to make some words in my form fields choice label bold, but I could not find how to do that. 

For example, I want  
Which one you would like to choose?
a. <b> Major </b> treatment
b. <b> Minor </b> treatment

The following codes does not work.
def make_expert_field(label):
    return models.BooleanField(
        choices=[[True, "<b> Major </b> Treatment"], [False, "<b> Minor </b>Treatment"]],

Could you please let me know how to put an HTML code into a python script?

Thank you very much.
Kind regards,

#2 by ccrabbe

Hi Pim -

In my experience, oTree doesn't allow for styling or html inside of its automatically-rendered template widgets.

There's two ways to get around this:

(1) put the html elements into your template yourself - the easiest way to do this is to view the source created by oTree, then copy-and-paste the generated html into your template in place of the {{ formfield }} template tag, and then edit it to have the formatting you want

(2) still use the formfield tag, but then add some javascript to your template (probably inside a window.onload function) which edits the oTree-generated html to add your formatting.

For form labels, it's much easier - you just set label='' and add that label text to the format right before the {{ formfield }} tag, and you can style it however you like.

Good luck,

#3 by gr0ssmann

I found the following workaround:

        $(document).ready(function (e) {
            labels = document.getElementsByTagName("label");

            for (var i = 0; i < labels.length; i++) {
                labels[i].innerHTML = labels[i].innerHTML.replaceAll("&lt;", "<").
                                      replaceAll("&gt;", ">");

Write a reply

Set forum username