Skip to content

Instantly share code, notes, and snippets.

@dcneiner
Created August 26, 2010 04:49
Show Gist options
  • Save dcneiner/550834 to your computer and use it in GitHub Desktop.
Save dcneiner/550834 to your computer and use it in GitHub Desktop.
// Original code from David Walsh
jQuery(document).ready(function() {
/* fetch elements */
jQuery('form.follow-form').each(function() {
/* stop form event */
jQuery(this).bind('submit',function(e) {
/* stop event */
e.preventDefault();
/* "on request" */
jQuery(this).find('i').addClass('active');
/* "save" button */
var button = jQuery(this).find('button');
/* send ajax request */
jQuery.post('twitter-follow.php',{
followID: jQuery(this).find('input').val()
},function() {
/* hide button, create element */
button.css('display','none').after('<span class="following"><span></span>Following!</span>');
});
});
});
});
// Idiomatic jQuery by Doug Neiner
jQuery(function ($) {
/* fetch elements and stop form event */
$("form.follow-form").submit(function (e) {
/* stop event */
e.preventDefault();
/* "on request" */
$(this).find('i').addClass('active');
/* send ajax request */
$.post('twitter-follow.php', {
followID: $(this).find('input').val()
}, function () {
/* find and hide button, create element */
$(e.currentTarget)
.find('button').hide()
.after('<span class="following"><span></span>Following!</span>');
});
});
});
// How I would actually write it (using live or delegate depending on context)
jQuery(function ($) {
/* fetch elements and stop form event */
$("form.follow-form").live("submit", function (e) {
// Everything else the same
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment