Skip to content

Instantly share code, notes, and snippets.

@ebaxt
Created March 4, 2012 19:09
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save ebaxt/1974391 to your computer and use it in GitHub Desktop.
Save ebaxt/1974391 to your computer and use it in GitHub Desktop.
Checking radio with Mustache
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title></title>
<script type="text/javascript" src="js/jquery-1.7.1.min.js"></script>
<script type="text/javascript" src="js/mustache.js"></script>
</head>
<body>
<div id="formWrapper">
</div>
<script id="tmpl" type="x-template">
<form>
{{#wrapped}}
<label><input type="radio" name="gender" value="male"> Male</label>
<label><input type="radio" name="gender" value="female"> Female</label>
{{/wrapped}}
</form>
</script>
<script type="text/javascript">
var data = {
gender: 'female',
wrapped:function () {
return function (text) {
return text.replace('value="' + this.gender+'"', 'value="' + this.gender+'" checked');
}
}
};
var template = $("#tmpl").html();
var result = Mustache.render(template, data);
$("#formWrapper").html(result);
</script>
</body>
</html>
@bobthecow
Copy link

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment