Skip to content

Instantly share code, notes, and snippets.

@stevekinney
Created December 7, 2015 17:21
Show Gist options
  • Save stevekinney/92e7bfde1c1e9a8f73bf to your computer and use it in GitHub Desktop.
Save stevekinney/92e7bfde1c1e9a8f73bf to your computer and use it in GitHub Desktop.
<!DOCTYPE html>
<html>
<head>
<meta name="charset" content="UTF-8">
<title>Buttons Upon Buttons</title>
<style>
button {
padding: 1em;
border: none;
}
.button {
background-color: purple;
color: white;
}
.add-new {
margin-top: 1em;
}
.add-new-button {
background-color: yellow;
}
</style>
</head>
<body>
<div class="buttons">
</div>
<div class="add-new">
<button class="add-new-button">Add New Button</button>
</div>
<script src="http://code.jquery.com/jquery-1.11.3.min.js"></script>
<script src="script.js"></script>
</body>
</html>
var $buttons = $('.buttons');
var $addNewButton = $('.add-new-button');
function createCounter() {
var count = 0;
return function () {
return ++count;
};
}
var nextId = createCounter();
function createTemplate(id) {
return `
<div class="idea">
<h2>Some Idea ${id}</h2>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
<button class="button">Delete Idea</button>
</div>
`
}
function addButtonToPage() {
var id = nextId();
$(createTemplate(id))
.find('.button')
.on('click', function () {
alert('DELETE /api/v1/' + id);
$(this).parent().remove();
})
.end()
.appendTo($buttons);
}
$addNewButton.on('click', addButtonToPage);
for (var i = 0; i < 5; i++) {
addButtonToPage();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment