Created
December 7, 2016 03:39
-
-
Save musamamasood/bf907195fe980b8ba6b316b2a27b3c28 to your computer and use it in GitHub Desktop.
jQuery plugin to prevent the empty search.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Stop empty searches | |
* | |
* @author Thomas Scholz http://toscho.de | |
* @param $ jQuery object | |
* @return bool|object | |
*/ | |
(function( $ ) { | |
$.fn.preventEmptySubmit = function( options ) { | |
var settings = { | |
inputselector: "#s", | |
msg : "Don’t waste your time with an empty search!" | |
}; | |
if ( options ) { | |
$.extend( settings, options ); | |
}; | |
this.submit( function() { | |
var s = $( this ).find( settings.inputselector ); | |
if ( ! s.val() ) { | |
alert( settings.msg ); | |
s.focus(); | |
return false; | |
} | |
return true; | |
}); | |
return this; | |
}; | |
})( jQuery ); | |
/* | |
* | |
* functions to call after include above plugin. | |
*/ | |
jQuery(document).ready( function( $ ){ | |
$( ".search_form" ).preventEmptySubmit({ /* search form selector */ | |
inputselector: "#search-text" /* search input selector.*/ | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment