Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*jshint esversion: 6 */
- function solve() {
- return function(selector) {
- if (typeof(selector) !== 'string' && !(selector instanceof HTMLElement)) {
- throw "The input is neither an element nor a string";
- }
- if (document.getElementById(selector) === null) {
- throw "Nothing selected.....";
- }
- // var buttons = document.getElementsByClassName('button');
- var content = document.getElementsByClassName("content");
- var contentAndButtons = document.querySelectorAll('.button, .content');
- // indexing click event on all buttons and adding Id for later
- for (var index = 0, len = contentAndButtons.length; index < len; index += 1) {
- if (contentAndButtons[i].className === "button") {
- contentAndButtons[index].innerHTML = 'hide';
- contentAndButtons[index].addEventListener('click', ButtonClicked, false);
- }
- contentAndButtons[index].id = index;
- }
- function ButtonClicked(button) {
- var buttonIndex = this.id;
- for (var i = buttonIndex, len = contentAndButtons.length; i < len; i++) {
- var element = contentAndButtons[i];
- var button = contentAndButtons[buttonIndex];
- if (element.className === "content") {
- if (element.style.display === '') {
- button.innerHTML = 'show';
- element.style.display = 'none';
- } else if (element.style.display === 'none') {
- element.style.display = '';
- button.innerHTML = 'hide';
- }
- break;
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement