Create a gist now

Instantly share code, notes, and snippets.

@acidsound /selectHelper.coffee Secret
Last active Aug 29, 2015

What would you like to do?
"select" HandleBars Helpers can set selected/checked attributes for Radio and Select.
Handlebars.registerHelper "select", (value, options) ->
select = document.createElement("div")
$(select).html options().replace(/<[/]*\$data:\w+>/g, "")
$("input[type=radio]", select).filter("[value='#{value}']").attr "checked", "checked"
$("option", select).filter("[value='#{value}']").attr "selected", "selected"
select.innerHTML
Handlebars.registerHelper("select", function(value, options) {
var html, select;
select = document.createElement("div");
$(select).html(options().replace(/<[/]*\$data:\w+>/g, ""));
$("input[type=radio]", select).filter("[value='" + value + "']").attr('checked', 'checked');
$("option", select).filter("[value='" + value + "']").attr('selected', 'selected');
return select.innerHTML;
});
Owner

acidsound commented Mar 10, 2014

usage1. radio

        <ul>
          {{#select specialistType}}
          {{#each codes "establishment"}}
          <li>
            <input type="radio" value="{{code}}"/>
            <label>{{name}}</label>
          </li>
          {{/each}}
          {{/select}}
        </ul>
Owner

acidsound commented Mar 10, 2014

usage2. select

          <select name="PH" id="PH">
          {{#select PH}}
          {{#each codes "PH"}}
            <option value="{{code}}">{{name}}</option>
          {{/each}}
          {{/select}}
          </select>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment