Skip to content

Instantly share code, notes, and snippets.

@acidsound
Last active August 29, 2015 13:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save acidsound/a7de8f12acafc787dff2 to your computer and use it in GitHub Desktop.
Save acidsound/a7de8f12acafc787dff2 to your computer and use it in GitHub Desktop.
"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;
});
@acidsound
Copy link
Author

usage1. radio

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

@acidsound
Copy link
Author

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