I'm just trying to learn how to write jQuery better so I wanted some smart peoples opinions.
The code is checking the billing and shipping address fields to see if it contains a PO Box address by looking for variations of "PO" and then displays a warning message after the input if it does contain it.
The code is duplicated for each one so there has to be a better way to write this.
(function ($) {
'use strict';
// Check for PO Box text in address fields on checkout
var $shipAddress = $('#checkout input[name="user_data[s_address]"]'),
$billAddress = $('#checkout input[name="user_data[b_address]"]'),
message = '<div class="message hidden"><p>We <strong>don\'t ship to PO Boxes</strong>. Sorry for the inconvenience.</p></div>';
$shipAddress.after(message);
$billAddress.after(message);
$shipAddress.keyup(function () {
var $value = $(this).val();
if ($value === 'po' || $value === 'Po' || $value === 'PO' || $value === 'P.O' || $value === 'p.o') {
$shipAddress.siblings('.message').removeClass('hidden');
} else if ($value === '') {
$shipAddress.siblings('.message').addClass('hidden');
}
});
$billAddress.keyup(function () {
var $value = $(this).val();
if ($value === 'po' || $value === 'Po' || $value === 'PO' || $value === 'P.O' || $value === 'p.o') {
$billAddress.siblings('.message').removeClass('hidden');
} else if ($value === '') {
$billAddress.siblings('.message').addClass('hidden');
}
});
}(jQuery));