I'm starting to do a lot of jQuery programming. I'm finding that my code is becoming a little hard to maintain and is not all that readable. My javascript is being included on every page of our web application, even though it is only used on one page. This has the benefits of having the javascript cached before loading the page, but I'm worried about accidentally creating functions with identical names (created by other programmers on my team).
I'm new to javascript, so there may be some basics that I'm missing. What are some techniques that I can apply to the following code, as well as other code in the future to make my jQuery code more maintainable and easy to read?
<script type="text/javascript">
$(document).ready(function(){
$('#registration-information .copy-button').click(copyField);
});
function copyField(e) {
e.preventDefault();
var $tr = $(this).closest('tr');
var originalText = jQuery.trim($tr.find('.contact-data').text());
var newText = jQuery.trim($tr.find('.registration-data').text());
var $button = $tr.find('.copy-button');
var $loadingIcon = $('<span class="loading-icon"></span>').hide().insertAfter($button);
var $undoLink = $('<a class="undo-link" href="#">Undo</a>').hide().insertAfter($button);
var field = $button.attr('id');
$undoLink.click(function(e){
e.preventDefault();
undoCopyField($tr, originalText);
});
$button.hide();
$loadingIcon.show();
$.ajax({
url: '/registrations/copy-field-to-contact',
data: {
id: '<?php echo $registration->ID ?>',
field: field,
data: newText
},
success: function(data){
if (data.success) {
$loadingIcon.hide();
$tr.find('.contact-data').html(newText);
$tr.find('td.form_field_label').removeClass('mismatched');
$tr.effect('highlight', 1000, function(){
$undoLink.fadeIn();
});
} else {
displayErrorMessage(data.error);
$loadingIcon.hide();
$button.show();
}
},
error: function(){
displayErrorMessage('Unknown reason');
$loadingIcon.hide();
$button.show();
}
});
}
function undoCopyField($tr, originalText) {
var $button = $tr.find('.copy-button');
var $loadingIcon = $tr.find('.loading-icon');
var $undoLink = $tr.find('.undo-link');
var field = $button.attr('id');
$undoLink.hide();
$loadingIcon.show();
$.ajax({
url: '/registrations/copy-field-to-contact',
data: {
id: '<?php echo $registration->ID ?>',
field: field,
data: originalText
},
success: function(data){
if (data.success) {
$undoLink.remove();
$loadingIcon.hide();
$tr.find('.contact-data').html(originalText);
$tr.find('td.form_field_label').addClass('mismatched');
$tr.effect('highlight', 1000, function(){
$tr.find('.copy-button').fadeIn();
});
} else {
displayErrorMessage(data.error);
$loadingIcon.hide();
$undoLink.show();
}
},
error: function(){
displayErrorMessage('Unknown reason');
$loadingIcon.hide();
$undoLink.show();
}
});
}
function displayErrorMessage(message) {
alert('Sorry, there was an error while trying to save your changes: ' + message);
}
</script>
Update: There are numerous sections of this code sample with chunks of code that are almost identical. Specifically the AJAX calls. Those two blocks are essentially the same, except for the actions that take place after the call has completed. I'd really like to figure out a way to DRY up those sections.