Guest User

Untitled

a guest
Jan 21st, 2018
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.64 KB | None | 0 0
  1. // disposable function wrapper that takes an element and monitors it for certain text and applies a class.
  2. // requires the class "untouched" to be defined as well. Call it once per item to be applied to.
  3. // should be called once on page load, expects parameter array like this:
  4. // [{ element: 'selector' },
  5. // { element: 'selector', text: 'text' },
  6. // { element: 'selector', text: 'text', class: 'someclass' },
  7. // { element: 'selector', text: 'text', class: 'someclass', callback: function(){} }
  8. // ]
  9. // class will use "untouched" if no class provided, and "Search" if no text provided.
  10. // Can also take a callback function and will pass the value of the element that's selected, and a reference to the element itself
  11. (function (elements) {
  12. for (var k in elements) {
  13. var e = elements[k];
  14. (function ($, id, text, class, cb) {
  15. $(function () {
  16. $(id)
  17. .click(Click)
  18. .blur(Blur)
  19. .keypress(Keypress)
  20. .keyup(Keyup)
  21. .blur();
  22. });
  23. function Click(event) {
  24. var that = $(this)
  25. if (that && that.hasClass(class) && that.val() === text) {
  26. that.removeClass(class).val('');
  27. }
  28. }
  29.  
  30. function Blur(event) {
  31. var that = $(this)
  32. if (that && !that.hasClass(class) && (that.val() === text || that.val() === '')) {
  33. that.addClass(class).val(text);
  34. }
  35. }
  36.  
  37. function Keypress(event) {
  38. var that = $(this)
  39. /* if they press enter */
  40. if (that && event.which === 13) {
  41. /* do something here to search for the value and redirect */
  42. if (cb && typeof(cb) === 'function'){
  43. cb.call(that,that.val());
  44. }
  45. }
  46. }
  47.  
  48. function Keyup(event) {
  49. var that = $(this)
  50. /* if they press esc */
  51. if (that && event.which === 27) {
  52. that.val('')
  53. }
  54. }
  55. })(window.jQuery, e.element, e.text || 'Search', e.class || 'untouched', e.callback || null);
  56. }
  57. })([
  58. { element: '#metername', text: 'Meter Number' },
  59. { element: '#startdate', text: 'From Date' },
  60. { element: '#enddate', text: 'To Date' }
  61. ]);
Add Comment
Please, Sign In to add comment