Skip to content

Instantly share code, notes, and snippets.

@emh
Created March 12, 2010 01:58
Show Gist options
  • Save emh/329963 to your computer and use it in GitHub Desktop.
Save emh/329963 to your computer and use it in GitHub Desktop.
Javascript Partials - because I hate seeing HTML embedded in my Javascript code.
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>javascript partials</title>
<script src="http://code.jquery.com/jquery-1.4.1.min.js" type="text/javascript"></script>
<script>
function createHello() {
var name = $('#name_field').attr('value');
var partial = $('#hello_partial').contents().clone();
partial.find('#hello_partial_name').html(name);
$('#container').prepend(partial);
}
</script>
</head>
<body>
<input value="Name" id="name_field"/>
<input type="submit" onclick="createHello()"/>
<hr/>
<div id="container">
</div>
<div id="hello_partial" style="display: none">
<div>
Hello <span id="hello_partial_name"></span>.
</div>
</div>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment