Skip to content

Instantly share code, notes, and snippets.

@tdouce
Created October 18, 2011 01:42
Show Gist options
  • Save tdouce/1294414 to your computer and use it in GitHub Desktop.
Save tdouce/1294414 to your computer and use it in GitHub Desktop.
amware - refactor #6
// When user clicks 'submit' button loop through all selects with a 'datetime' class.
// Find the select element that furtherest DOWN the DOM.
// Add a warning to any other select elememnt that has an empty value that
// is further UP the DOM
$(".commit").click( function(e){
// Html warning to append. Create a unique class name ('javascript_warning')
// to faciliate identifying and removing ONLY the warnings thata are added with this function.
// NOTE: We are piggy-backing on the existing css for the errors by using the 'inline-errors' class.
var warning_html = '<p class="inline-errors javascript_warning"> Warning, this is blank </p>';
// Remove any previous javascript warnings that we added with this function ONLY
$('.javascript_warning').remove();
// Flag for identifying whether or not we should add a warning.
var post_warning = false;
// Get all the elements and reverse them so we can iterate backwards through the DOM
$($( '.datetime' ).get().reverse()).each( function(){
// Find the select element that furtherest DOWN the DOM. Add a warning to any
// other select elememnt that has an empty value that is further UP the DOM
$(this).find('select').each(function(){
// If select element has a value then change the flag to 'true'
if ( $(this).val() != "" )
{
post_warning = true;
}
// If select element is blank and the flag has already been set to
// 'true', then add a warning, stop form submission, and break out of
// loop so only one warning is added (rather than 5)
else if( $(this).val() == "" & post_warning == true )
{
$(this).closest('.datetime').append( warning_html );
e.preventDefault();
return false;
};
});
});
// When user clicks 'submit' button loop through all selects with a 'datetime' class.
// Find the select element that furtherest DOWN the DOM.
// Add a warning to any other select elememnt that has an empty value that
// is further UP the DOM and stop the form submission.
$(".commit").click( function(e){
// Html warning to append. Create a unique class name ('javascript_warning')
// to faciliate identifying and removing ONLY the warnings thata are added with this function.
// NOTE: We are piggy-backing on the existing css for the errors by using the 'inline-errors' class.
var warning_html = '<p class="inline-errors javascript_warning"> Warning, this is blank </p>';
// Remove any previous javascript warnings that we added with this function ONLY
$('.javascript_warning').remove();
// Flag for identifying whether or not we should add a warning.
var post_warning = false;
// Get all the elements and reverse them so we can iterate backwards through the DOM
$($( '.datetime' ).get().reverse()).each( function(){
// Find the select element that furtherest DOWN the DOM. Add a warning to any
// other select elememnt that has an empty value that is further UP the DOM
$(this).find('select').each(function(){
// If select element has a value then change the flag to 'true'
if ( $(this).val() != "" )
{
post_warning = true;
}
// If select element is blank and the flag has already been set to
// 'true', then add a warning, stop form submission, and break out of
// loop so only one warning is added (rather than 5)
else if( $(this).val() == "" & post_warning == true )
{
$(this).closest('.datetime').append( warning_html );
e.preventDefault();
return false;
};
});
});
@wallace
Copy link

wallace commented Oct 18, 2011

NICE!

@tdouce
Copy link
Author

tdouce commented Oct 19, 2011 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment