oTree Forum >

create an open-field

#1 by galbitton

Hi everyone, 

I want to create a variable that asks people for their ethnicity. This string variable gets 6 values for the different ethnic groups, another for "other", and the third for "refuse to answer". 

How can I attach to the value "other" an open field option so participant can write their ethnic group if clicking on "other"?

Appreciate your help!

Gal

#2 by gr0ssmann ,

The following "sketch" should give you a hint:

    <select onchange="select_change(this)">
        <option disabled selected>Please select:</option>
        <option>Option 1</option>
        <option>Option 2</option>
        <option>Option 3</option>
        <option>Other</option>
        <option>Prefer not to say</option>
    </select>

    <div id="other_wrap" style="display:none">
        <p>Other:</p>

        <input type="text" id="other_value" name="other_value" onchange="change_field(this.value)" />
    </div>

    <input type="hidden" name="actual_field_name" id="actual_field_name" value="" />

    <script>
        function select_change(el) {
            change_field(el.value);

            if (el.value == "Other") {
                change_field("");
                document.getElementById("other_value").value = "";
                document.getElementById("other_wrap").style.display = "block";
            }
            else {
                document.getElementById("other_wrap").style.display = "none";
            }
        }

        function change_field(val) {
            document.getElementById("actual_field_name").value = val;
        }
    </script>

Remember to replace actual_field_name with the field name defined in the Player class.

Write a reply

Set forum username