My question is straightforward. This is my code, and I find that they are two similar how can I improve/make this code shorter?
I find they are similar at many points, that is why I am asking here.
Here is my code and any help is appreciated.
$(".follow-link").click(function(event) {
event.preventDefault();
var therel = $(this).attr('rel');
var followID = $(this).attr('rel').replace(/[^0-9]/g, '');
var thisfollow = $(this);
$.ajax({
url: '/ajax/follow.php',
type: 'POST',
data: {followwho : followID},
dataType: 'json',
success: function(data){
if (data.status) {
$('a[rel="' + therel + '"]').hide();
$('a[rel="' + therel + '"]').parent().children('.unfollow-link').fadeIn();
}
}
});
});
$(".unfollow-link").click(function(event) {
event.preventDefault();
var therel = $(this).attr('rel');
var followID = $(this).attr('rel').replace(/[^0-9]/g, '');
var thisfollow = $(this);
$.ajax({
url: '/ajax/unfollow.php',
type: 'POST',
data: {followwho : followID},
dataType: 'json',
success: function(data){
if (data.status) {
$('a[rel="' + therel + '"]').hide();
$('a[rel="' + therel + '"]').parent().children('.follow-link').fadeIn();
}
}
});
});
Make one common function and do some simplification cleanup work in that function: