lib-jquery-learning-bookstore-scripts-forms.js / js
************************************* Contact form --------------------------------------
document.ready(function() { // Enhance style of form elements. $('legend').each(function(index) { this.replaceWith('<h3>' + this.text() + '</h3>'); }); // $('legend').wrapInner('<span></span>'); var requiredFlag = ' * '; var conditionalFlag = ' ** '; var requiredKey = $('input.required:first') .next('span').text(); var conditionalKey = $('input.conditional:first') .next('span').text(); requiredKey = requiredFlag + requiredKey.replace(/^\((.+)\)<p></p>') .addClass('field-keys') .append(requiredKey + '<br />') .append(conditionalKey) .insertBefore('#contact'); $('form :input') .filter('.required') .next('span').text(requiredFlag).end() .prev('label').addClass('req-label').end() .end() .filter('.conditional') .next('span').text(conditionalFlag); // Checkbox toggle: conditional text inputs. $('input.conditional').next('span').andSelf().hide() .end().end() .each(function() { var thisInput = this; var thisInput.next('span'); thisInput.show().addClass('required'); thisInput.hide().removeClass('required').blur(); // Validate fields on blur. $('form :input').blur(function() { this.parents('li:first').removeClass('warning') .find('span.error-message').remove(); if (this.hasClass('required')) { var listItem = this.parents('li:first'); if (this.value == '') { var errorMessage = 'This is a required field'; if (this.is('.conditional')) { errorMessage += ', when its related ' + 'checkbox is checked'; } $('<span></span>') .addClass('error-message') .text(errorMessage) .appendTo(listItem.addClass('warning'); } } if (this.id == 'email') { var /.test(this.value)) { var errorMessage = 'Please use proper e-mail format' + ' (e.g. joe@example.com)'; $('<span></span>') .addClass('error-message') .text(errorMessage) .appendTo(listItem.addClass('warning'); } } }); // Validate form on submit. $('form').submit(function() { $('#submit-message').remove(); $(':input.required').trigger('blur'); var numWarnings = $('.warning', this).length; if (numWarnings) { var fieldList = []; $('.warning label').each(function() { fieldList.push(this.text()); }); $('<div></div>') .attr({ 'id': 'submit-message', 'class': 'warning' }) .append('Please correct errors with the following ' + numWarnings + ' fields:<br />') .append('• ' + fieldList.join('<br />• ')) .insertBefore('#send'); return false; }; }); // Checkboxes $('form :checkbox').removeAttr('checked'); // Checkboxes with (un)check all. $('<li></li>') .html('<label><input type="checkbox" id="discover-all" />' + ' <em>check all</em></label>') .prependTo('li.discover > ul'); $('#discover-all').click(function() { var checkboxes.attr('checked', true); } else { this.next().text(' check all'); }; }) .parent('label').addClass('checkall'); });
************************************* Insert placeholder text --------------------------------------
document.ready(function() { var search = $('#search').addClass('overlabel'); var search.find('input'); var search.find('label'); if (searchLabel.hide(); } searchLabel.hide(); }) .blur(function() { if (this.value == '') { searchLabel.click(function() {
************************************* Autocomplete search field --------------------------------------
document.ready(function() { var autocomplete = $('<ul class="autocomplete"></ul>') .hide() .insertAfter('#search-text'); var selectedItem = null; var setSelectedItem = function(item) { selectedItem = item; if (selectedItem === null) { autocomplete.find('li').length) { selectedItem = autocomplete.find('li').removeClass('selected') .eq(selectedItem).addClass('selected'); }; var populateSearchField = function() { $('#search-text').val(autocomplete .find('li').eq(selectedItem).text()); setSelectedItem(null); }; $('#search-text') .attr('autocomplete', 'off') .keyup(function(event) { if (event.keyCode > 40 || event.keyCode == 8) { // Keys with codes 40 and below are special (enter, arrow keys, escape, etc.). // Key code 8 is backspace. autocomplete.empty(); <li></li>').text(term).appendTo(autocomplete).mouseover(function() { setSelectedItem(index); }).click(populateSearchField); }); setSelectedItem(0); } else { setSelectedItem(null); } } }); } else if (event.keyCode == 38 && selectedItem !== null) { // User pressed up arrow. setSelectedItem(selectedItem - 1); event.preventDefault(); } else if (event.keyCode == 40 && selectedItem !== null) { // User pressed down arrow. setSelectedItem(selectedItem + 1); event.preventDefault(); } else if (event.keyCode == 27 && selectedItem !== null) { // User pressed escape key. setSelectedItem(null); } }).keypress(function(event) { if (event.keyCode == 13 && selectedItem !== null) { // User pressed enter key. populateSearchField(); event.preventDefault(); } }).blur(function(event) { setTimeout(function() { setSelectedItem(null); }, 250); }); });
************************************* Shopping cart --------------------------------------
document.ready(function() { var stripe = function() { $('#cart tbody tr').removeClass('alt') .filter(':visible:odd').addClass('alt'); }; stripe(); $('#recalculate').hide(); $('.quantity input').keypress(function(event) { if (event.which && (event.which < 48 || event.which > 57)) { event.preventDefault(); } }).change(function() { var totalQuantity = 0; var totalCost = 0; $('#cart tbody tr').each(function() { var price = parseFloat($('.price', this) .text().replace(/^[^\d.]*/, '')); price = isNaN(price) ? 0 : price; var quantity = parseInt($('.quantity input', this).val(), 10); quantity = isNaN(quantity) ? 0 : quantity; var cost = quantity * price; $('.cost', this).text('' + totalCost.toFixed(2)); var taxRate = parseFloat($('.tax .price').text()) / 100; var tax = Math.ceil(totalCost * taxRate * 100) / 100; $('.tax .cost').text('' + shipping.toFixed(2)); totalCost += shipping; $('.total .cost').text('<th> </th>') .insertAfter('#cart thead th:nth-child(2)'); $('#cart tbody tr').each(function() { deleteButton = $('<img />').attr({ 'width': '16', 'height': '16', 'src': '../images/cross.png', 'alt': 'remove from cart', 'title': 'remove from cart', 'class': 'clickable' }).click(function() { this.parents('tr').find('td.quantity input') .val(0).trigger('change') .end().hide(); stripe(); }); $('<td></td>') .insertAfter($('td:nth-child(2)', this)) .append(<td> </td>') .insertAfter('#cart tfoot td:nth-child(2)'); });
************************************* Edit Shipping Information --------------------------------------
document.ready(function() { var editShipping = function() { .get('shipping.php', function(data) { $('#shipping-name').remove(); data.hide().appendTo('#shipping').slideDown(); $('#shipping form').submit(saveShipping); }); return false; }; var saveShipping = function() { var postData = this.serialize() + '&op=Save'; }; $('#shipping-name').click(editShipping); });
(C) Æliens 20/2/2008
You may not copy or print any of this material without explicit permission of the author or the publisher. In case of other copyright issues, contact the author.