Skip to content

Instantly share code, notes, and snippets.

@makgithub
Last active April 7, 2017 07:51
Show Gist options
  • Save makgithub/bb07dd940b8b4488118818f6c94f981b to your computer and use it in GitHub Desktop.
Save makgithub/bb07dd940b8b4488118818f6c94f981b to your computer and use it in GitHub Desktop.
Fill template using jquery. Defining a HTML template to append using JQuery
<!DOCTYPE html>
<html>
<head>
<title>Modular Design Pattern</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<div>TODO write content
<input type="button" name="button" value="buttonclick" id="buttonclick"/>
<div id="target"></div>
</div>
</body>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
<script>
$(document).ready(function () {
//Clone the template
var template = $('#hidden-template').html();
$('#buttonclick').click(function () {
//Clone the template
var item = $(template).clone();
//Find the
$(item).find('.first').html("First");
//Change 'bar' to '-Bar'
$(item).find('#bar').html("-Bar");
//Append to the source
$('#target').append(item);
});
});
</script>
<script id="hidden-template" type="text/x-custom-template">
<tr>
<td class="first">Foo</td>
<td id="bar">Bar</td>
<tr>
</script>
</html>
@makgithub
Copy link
Author

jquery template

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