I have taken a stab at creating my first jQuery script from scratch. I use this to post an update to a Django application that allows a user to "follow" another user. I am sure that there are better ways to do this and I appreciate any help that anyone can give.
<script type="text/javascript">
jQuery(function() {
jQuery('.follow').click(function(event) {
event.preventDefault();
var url = jQuery(this).attr("href");
var followtype = jQuery(this).text();
var user = jQuery(".follow").attr("href").match('/relationships/[a-z]+/([a-z]+)/following/')[1];
var postdata = {
'csrfmiddlewaretoken': '{{ csrf_token }}'
}
jQuery.post(url, postdata, function(json){
if(json.result == '1') {
if(followtype == 'Follow') {
jQuery('.follow').text('Unfollow');
jQuery('.follow').attr("href", '/relationships/remove/'+user+'/following/');
} else {
jQuery('.follow').text('Follow');
jQuery('.follow').attr("href", '/relationships/add/'+user+'/following/');
}
}
})
});
});
</script>