Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function solve () {
- function isHTMLElement(obj){
- return obj === 'object' && obj instanceof HTMLElement;
- }
- function isString(obj){
- return obj === 'string' || obj instanceof String;
- }
- function isExistingId(id){
- var element = document.getElementById('id');
- return element !== 'undefine' && element !== null;
- }
- var validator = {
- validateParams: function (selector){
- if(!isString(selector) && !isHTMLElement(selector)){
- throw new Error('The provided id is not a string or does not select any DOM element!');
- }
- }
- validateExistingId: function (id){
- if (!isExistingId(id)) {
- throw new Error('The provided id does not select anything!');
- }
- }
- }
- return function (selector) {
- // Validations
- validator.validateParams(selector);
- validator.validateExistingId(selector);
- // Select all buttons and change their content to 'hide'
- var buttons = document.getElementsByClassName('button'),
- i,len;
- for(i = 0; len = buttons.length; i < len; i+=1){
- buttons[i].innerHTML = 'hide';
- }
- // Iterate over all buttons and implement onclick functionality
- for(i = 0; len = buttons.length; i < len; i+=1){
- var currButton = buttons[i];
- currButton.addEventListener('click', function(ev){
- var clickedButton = ev.target;
- var nextContent = clickedButton.nextElementSibling;
- while(nextContent && nextContent.className.indexOf('content') < 0){
- nextContent = nextContent.nextElementSibling;
- }
- var isContentVisible = nextContent.style.display === ''; // <=> display:block
- if (isContentVisible) {
- nextContent.style.display = 'none';
- clickedButton.innerHTML = 'show';
- }else{
- nextContent.style.display = ''; // <=> display:block
- clickedButton.innerHTML = 'hide';
- }
- });
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement