Could someone please help me make this code as professional as it can be? It works fine, but I feel there's a better way of doing this. I would really appreciate some guidance so I learn to code better.
$(function () {
$('#subForm').submit(function (e) {
e.preventDefault();
$.getJSON(
this.action + "?callback=?",
$(this).serialize(),
function (data) {
if (data.Status === 400) {
$('#slidemarginleft p').append("" + data.Message);
$("#slidemarginleft p").append('<button class="css3button wee">OK, I'll try again</button>');
$(function() {
var $marginLefty = $('.inner');
$marginLefty.animate({
marginLeft: parseInt($marginLefty.css('marginLeft'),10) == 0 ?
$marginLefty.outerWidth() : 0
});
});
//add click functionality for the button
$('#slidemarginleft button.wee').click(function() {
var $marginLefty = $('.inner');
$marginLefty.animate({
marginLeft: parseInt($marginLefty.css('marginLeft'),10) == 0 ?
$marginLefty.outerWidth() : 0
});
setTimeout(function() {
$('#slidemarginleft p').empty();}, 500);
});
} else { // 200
$(function() {
var $marginLefty = $('.inner');
$marginLefty.animate({
marginLeft: parseInt($marginLefty.css('marginLeft'),10) == 0 ?
$marginLefty.outerWidth() :
0
});
});
$('#slidemarginleft p').append("Thank you. " + data.Message);
}
});
});
if ($.browser.webkit) { $input.css('marginTop', 1); }
});
I have amended the code to this now (watching my indentation, adding a function, moving the default behaviour down but I think I have done it incorrectly as it doesn't work now):
$(function () {
"use strict";
$('#subForm').submit(function (e) {
function moveDiv() {
var $marginLefty = $('.inner');
$marginLefty.animate({
marginLeft: parseInt($marginLefty.css('marginLeft'), 10) === 0 ?
$marginLefty.outerWidth() : 0
});
}
$.getJSON(
this.action + "?callback=?",
$(this).serialize(),
function (data) {
if (data.Status === 400) {
$('#slidemarginleft p').append(" " + data.Message);
$("#slidemarginleft p").append('<button class="css3button wee">OK, I'll try again</button>');
$.change(moveDiv);
//add click functionality for the button
$('#slidemarginleft button.wee').click.change(moveDiv);
setTimeout(function () {
$('#slidemarginleft p').empty();
}, 500);
} else { // 200
$.change(moveDiv);
$('#slidemarginleft p').append("Thank you. " + data.Message);
}
});
e.preventDefault();
});
//if ($.browser.webkit) { $.browser.input.css('marginTop', 1); }
});
Here is the final code that works like a dream:
$(function () {
"use strict";
$('#subForm').submit(function (e) {
function moveDiv() {
var $marginLefty = $('.inner');
$marginLefty.animate({
marginLeft: parseInt($marginLefty.css('marginLeft'), 10) === 0 ?
$marginLefty.outerWidth() : 0
});
}
$.getJSON(
this.action + "?callback=?",
$(this).serialize(),
function (data) {
if (data.Status === 400) {
$('#slidemarginleft p').append(" " + data.Message);
moveDiv();
//add click functionality for the button
$('#slidemarginleft button.wee').click(function () {
moveDiv();
setTimeout(function () {
$('#slidemarginleft p').empty();
}, 500);
});
} else { // 200
moveDiv();
$('#slidemarginleft p').append("Thank you. " + data.Message);
}
});
e.preventDefault();
});
//if ($.browser.webkit) { $.browser.input.css('marginTop', 1); }
});
moveDiv()
instead of$.change(moveDiv)
; \$\endgroup\$