Skip to content

Instantly share code, notes, and snippets.

@libbyschuknight
Created August 10, 2015 03:02
Show Gist options
  • Save libbyschuknight/07e92cb035c586479595 to your computer and use it in GitHub Desktop.
Save libbyschuknight/07e92cb035c586479595 to your computer and use it in GitHub Desktop.
Example code for ajax calls for Hacker News
$(document).ready(function() {
$(document).on('click', '.voted', function(e){
e.preventDefault();
})
$(document).on('click', '.vote-button', function (e){
e.preventDefault();
// console.log($this)
var $link = $(this);
var url = $link.attr('href');
$.ajax ({
type : "GET",
url : url,
success : function (data){
console.log("success data", data);
var post_data = JSON.parse(data);
console.log(post_data)
var $points = $link.parent().find('.points');
// console.log($points)
var newPoints = post_data.count;
$points.text(newPoints);
// id - find vote-button and turn off??
$link.addClass('voted').css({
'color' : 'red',
'text-decoration' : 'none'
});
$link.removeClass('vote-button');
},
error : function (error){
console.log("Error: ", error);
}
});
});
$(document).on('click', '.delete', function(e){
e.preventDefault();
var $link = $(this);
var url = $link.attr('href');
$.ajax({
type: "DELETE",
url: url,
success: function(data){
var post_data = JSON.parse(data);
var id = post_data.id;
$('#' + id).remove();
},
error : function (error){
console.log("Error: ", error);
}
});
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment