bhalash

Example JS object

Dec 30th, 2014
201
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.     var navSearch = {
  2.         button: '#search input:submit',
  3.         input: '#search input:text',
  4.         placeholder: 'Type here to search...',
  5.         openWidth: 300,
  6.         closedWidth: 50,
  7.         openSearch: function() {
  8.             $(this.input).css('width', this.openWidth);
  9.         },
  10.         closeSearch: function() {
  11.             $(this.input).attr('placeholder', '');
  12.  
  13.             if ($(this.input).val().length == 0) {
  14.                 $(this.input).css('width', this.closedWidth);
  15.             }
  16.         },
  17.         setPlaceholder: function() {
  18.             if ($(this.input).val().length == 0) {
  19.                 $(this.input).attr('placeholder', this.placeholder);
  20.             }
  21.         },
  22.         isOpen: function() {
  23.             if (!$(this.input).is(':focus')
  24.                 && $(this.input).val().length == 0
  25.                 && $(this.input).outerWidth() == this.closedWidth) {
  26.                 return false;
  27.             }
  28.  
  29.             return true;
  30.         }
  31.     };
  32.  
  33.     $(navSearch.input).focus(function() {
  34.         navSearch.setPlaceholder();
  35.     });
  36.  
  37.     $(navSearch.input).blur(function() {
  38.         navSearch.closeSearch();
  39.     });
  40.  
  41.     $(navSearch.button).click(function() {
  42.         if (!navSearch.isOpen()) {
  43.             $(navSearch.input).focus();
  44.             navSearch.openSearch();
  45.             return false;
  46.         } else if ($(navSearch.input).val().length > 0) {
  47.             // Submit the search. TODO
  48.             console.log($(navSearch.input).val());
  49.             return false;
  50.         }
  51.     });
Advertisement
Add Comment
Please, Sign In to add comment