Advertisement
Guest User

Untitled

a guest
Aug 4th, 2015
180
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.80 KB | None | 0 0
  1. function solve () {
  2.  
  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.  
  30. return function (selector) {
  31. // Validations
  32. validator.validateParams(selector);
  33. validator.validateExistingId(selector);
  34.  
  35. // Select all buttons and change their content to 'hide'
  36. var buttons = document.getElementsByClassName('button'),
  37. i,len;
  38.  
  39. for(i = 0; len = buttons.length; i < len; i+=1){
  40. buttons[i].innerHTML = 'hide';
  41. }
  42.  
  43. // Iterate over all buttons and implement onclick functionality
  44. for(i = 0; len = buttons.length; i < len; i+=1){
  45. var currButton = buttons[i];
  46.  
  47. currButton.addEventListener('click', function(ev){
  48. var clickedButton = ev.target;
  49. var nextContent = clickedButton.nextElementSibling;
  50. while(nextContent && nextContent.className.indexOf('content') < 0){
  51. nextContent = nextContent.nextElementSibling;
  52. }
  53.  
  54. var isContentVisible = nextContent.style.display === ''; // <=> display:block
  55. if (isContentVisible) {
  56. nextContent.style.display = 'none';
  57. clickedButton.innerHTML = 'show';
  58. }else{
  59. nextContent.style.display = ''; // <=> display:block
  60. clickedButton.innerHTML = 'hide';
  61. }
  62.  
  63. });
  64.  
  65. }
  66. }
  67. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement