Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // disposable function wrapper that takes an element and monitors it for certain text and applies a class.
- // requires the class "untouched" to be defined as well. Call it once per item to be applied to.
- // should be called once on page load, expects parameter array like this:
- // [{ element: 'selector' },
- // { element: 'selector', text: 'text' },
- // { element: 'selector', text: 'text', class: 'someclass' },
- // { element: 'selector', text: 'text', class: 'someclass', callback: function(){} }
- // ]
- // class will use "untouched" if no class provided, and "Search" if no text provided.
- // Can also take a callback function and will pass the value of the element that's selected, and a reference to the element itself
- (function (elements) {
- for (var k in elements) {
- var e = elements[k];
- (function ($, id, text, class, cb) {
- $(function () {
- $(id)
- .click(Click)
- .blur(Blur)
- .keypress(Keypress)
- .keyup(Keyup)
- .blur();
- });
- function Click(event) {
- var that = $(this)
- if (that && that.hasClass(class) && that.val() === text) {
- that.removeClass(class).val('');
- }
- }
- function Blur(event) {
- var that = $(this)
- if (that && !that.hasClass(class) && (that.val() === text || that.val() === '')) {
- that.addClass(class).val(text);
- }
- }
- function Keypress(event) {
- var that = $(this)
- /* if they press enter */
- if (that && event.which === 13) {
- /* do something here to search for the value and redirect */
- if (cb && typeof(cb) === 'function'){
- cb.call(that,that.val());
- }
- }
- }
- function Keyup(event) {
- var that = $(this)
- /* if they press esc */
- if (that && event.which === 27) {
- that.val('')
- }
- }
- })(window.jQuery, e.element, e.text || 'Search', e.class || 'untouched', e.callback || null);
- }
- })([
- { element: '#metername', text: 'Meter Number' },
- { element: '#startdate', text: 'From Date' },
- { element: '#enddate', text: 'To Date' }
- ]);
Add Comment
Please, Sign In to add comment