Advertisement
Guest User

Untitled

a guest
Aug 4th, 2015
165
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.76 KB | None | 0 0
  1. function solve () {
  2. return function (selector) {
  3. function isHTMLElement(obj){
  4. return obj === 'object' && obj instanceof HTMLElement;
  5. }
  6.  
  7. function isString(obj){
  8. return obj === 'string' || obj instanceof String;
  9. }
  10.  
  11. function isExistingId(id){
  12. var element = document.getElementById('id');
  13. return element !== 'undefine' && element !== null;
  14. }
  15.  
  16. var validator = {
  17. validateParams: function (selector){
  18. if(!isString(selector) && !isHTMLElement(selector)){
  19. throw new Error('The provided id is not a string or does not select any DOM element!');
  20. }
  21. }
  22.  
  23. validateExistingId: function (id){
  24. if (!isExistingId(id)) {
  25. throw new Error('The provided id does not select anything!');
  26. }
  27. }
  28. }
  29. // Validations
  30. validator.validateParams(selector);
  31. validator.validateExistingId(selector);
  32.  
  33. // Select all buttons and change their content to 'hide'
  34. var buttons = document.getElementsByClassName('button'),
  35. i,len;
  36.  
  37. for(i = 0; len = buttons.length; i < len; i+=1){
  38. buttons[i].innerHTML = 'hide';
  39. }
  40.  
  41. // Iterate over all buttons and implement onclick functionality
  42. for(i = 0; len = buttons.length; i < len; i+=1){
  43. var currButton = buttons[i];
  44.  
  45. currButton.addEventListener('click', function(ev){
  46. var clickedButton = ev.target;
  47. var nextContent = clickedButton.nextElementSibling;
  48. while(nextContent && nextContent.className.indexOf('content') < 0){
  49. nextContent = nextContent.nextElementSibling;
  50. }
  51.  
  52. var isContentVisible = nextContent.style.display === ''; // <=> display:block
  53. if (isContentVisible) {
  54. nextContent.style.display = 'none';
  55. clickedButton.innerHTML = 'show';
  56. }else{
  57. nextContent.style.display = ''; // <=> display:block
  58. clickedButton.innerHTML = 'hide';
  59. }
  60.  
  61. });
  62.  
  63. }
  64. }
  65. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement