Skip to content

Instantly share code, notes, and snippets.

@ayoformayo
Created July 8, 2013 15:58
Show Gist options
  • Save ayoformayo/5950091 to your computer and use it in GitHub Desktop.
Save ayoformayo/5950091 to your computer and use it in GitHub Desktop.
function Comment(author, text){
this.form = "<form id='new_comment'><textarea></textarea><input name='author' id='authorName' type='text'><input type='submit' value='submit' id='submitButton'></form>";
this.author = author;
this.text = text;
};
Comment.prototype.addToPage = function() {
$('#comment_list').append("<li>" + this.text + "<span class='author'>" + this.author + "</span></li>");
$('#new_comment').replaceWith('<button id="new_comment_button">New Comment</button>');
};
$(document).ready(function(){
$(document).on('click','#new_comment_button', function(){
var comment = new Comment;
$('#new_comment_button').replaceWith(comment.form);
});
$(document).on('submit', '#new_comment', function(event){
event.preventDefault();
var newComment = new Comment();
newComment.author = $('input[name="author"]').val();
newComment.text = $('textarea').val();
newComment.addToPage();
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment