You will need to do this using Javascript. Basically, you can just add it to the change event. If you use the default Simple_form form builder, then the help field should appear as a <span> next to the input field itself , that is, you should see something like the following when you open the page source
<div class="input string optional property_surface_area field_with_hint"> <label class="string optional" for="property_surface_area">Surface Area</label> <input class="string optional" type="text" value="" name="property[surface_area]" id="property_surface_area" /> <span class="hint"></span> </div>
So, the task is to update the span hint value in the change event to select units. The following code does this:
switchUnit: -> $(document).on 'change', '#property_area_unit', -> areaInput = $('#property_surface_area') areaHint = $('.property_area_unit span.hint') if $(this).val() == 'sq_m' area = Math.round(parseFloat(areaInput.val()) * 0.0929 * 100) / 100 areaInOtherUnits = Math.round(area * 10.7639 * 100) / 100 else area = Math.round(parseFloat(areaInput.val()) * 10.7639 * 100) / 100 areaInOtherUnits = Math.round(area * 0.0929 * 100) / 100 areaInput.val(area) areaHint.html('= ' + areaInOtherUnits + ($(this).val() == 'sq_m' ? ' sq feet' : ' sq m'))
The code above recalculates the area back to other units (you could probably just use the original value from the input property_surface_area , but recounting also rounds it) and sets the contents of the tooltip for that value, including other units. Selector for tooltip: "wrapper div for the entire form line → span with class" tooltip ".
source share