Advertisement
svetlai

jQuery task 2

Aug 1st, 2015
233
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /* globals $ */
  2.  
  3. /*
  4. Create a function that takes a selector and:
  5. * Finds all elements with class `button` or `content` within the provided element
  6.   * Change the content of all `.button` elements with "hide"
  7. * When a `.button` is clicked:
  8.   * Find the topmost `.content` element, that is before another `.button` and:
  9.     * If the `.content` is visible:
  10.       * Hide the `.content`
  11.       * Change the content of the `.button` to "show"      
  12.     * If the `.content` is hidden:
  13.       * Show the `.content`
  14.       * Change the content of the `.button` to "hide"
  15.     * If there isn't a `.content` element **after the clicked `.button`** and **before other `.button`**, do nothing
  16. * Throws if:
  17.   * The provided ID is not a **jQuery object** or a `string`
  18.  
  19. */
  20. function solve() {
  21.   return function (selector) {
  22.  
  23.     if ($(selector).length === 0 || typeof (selector) !== 'string') {
  24.       throw new Error();
  25.     }
  26.  
  27.     var $domElement = $(selector);
  28.     $domElement.find('.button')
  29.         .html('hide');
  30.  
  31.     //console.log($domElement.html());
  32.  
  33.     $domElement.on('click', '.button', function () {
  34.       var $clickedButton = $(this);
  35.       var $content = $clickedButton.next();
  36.  
  37.       while($content && !$content.hasClass('button')){
  38.         if($content.hasClass('content')){
  39.           if($content.css('display') == 'none'){
  40.             //console.log('show yourself!!!');
  41.             $clickedButton.html('hide');
  42.             //$content.hide();
  43.             $content.css('display', '');
  44.           } else {
  45.             $clickedButton.html('show');
  46.             //$content.show();
  47.             $content.css('display', 'none');
  48.           }
  49.  
  50.           //$content.toggle();
  51.  
  52.           break;
  53.         }
  54.  
  55.         $content = $content.next();
  56.       }
  57.     })
  58.   };
  59. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement