Show hide content based on button click

Viewed 83

How to show/hide content based on button click?

I am able to achieve this as shown below but my code is very long which and am sure there is much better/shorter way to do it.

HTML

<p class="option2-top">option two</p>
<p class="option3-top">option three</p>

<button id="option-one">one</button>
<button id="option-two">two</button>
<button id="option-three">three</button>

<p class="option1-below">option one</p>
<p class="option2-below">option two</p>
<p class="option3-below">option three</p>

Javascript

var oneTop = document.querySelector(".option1-top");
var twoTop = document.querySelector(".option2-top");
var threeTop = document.querySelector(".option3-top");

var oneBelow = document.querySelector(".option1-below");
var twoBelow = document.querySelector(".option2-below");
var threeBelow = document.querySelector(".option3-below");

oneTop.style.display = "block";
twoTop.style.display = "none";
threeTop.style.display = "none";

oneBelow.style.display = "block";
twoBelow.style.display = "none";
threeBelow.style.display = "none";

document.getElementById("option-one").onclick = function () {
oneTop.style.display = "block";
twoTop.style.display = "none";
threeTop.style.display = "none";

oneBelow.style.display = "block";
twoBelow.style.display = "none";
threeBelow.style.display = "none";
};

document.getElementById("option-two").onclick = function () {
oneTop.style.display = "none";
twoTop.style.display = "block";
threeTop.style.display = "none";

oneBelow.style.display = "none";
twoBelow.style.display = "block";
threeBelow.style.display = "none";
};

document.getElementById("option-three").onclick = function () {
oneTop.style.display = "none";
twoTop.style.display = "none";
threeTop.style.display = "block";

oneBelow.style.display = "none";
twoBelow.style.display = "none";
threeBelow.style.display = "block";
};

Here is a working JSfiddle

I don't mind a jQuery solution!

4 Answers

    $(document).ready(function(){
     $(".opt").hide();
     $("button").click(function(){
       $("p").hide();
       class_nmae = $(this).attr('id');
       $("."+class_nmae).show();
     });
   }); 
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<p class="option-one">option one</p>
    <p class="option-two opt">option two</p>
    <p class="option-three opt">option three</p>
    <p class="option-four opt">option four</p>

    <button id="option-one">one</button>
    <button id="option-two">two</button>
    <button id="option-three">three</button>
    <button id="option-four">four</button>

    <p class="option-one">option one</p>
    <p class="option-two opt">option two</p>
    <p class="option-three opt">option three</p>
    <p class="option-four opt">option four</p>

 you can use like this
    <p class="option-one">option one</p>
    <p class="option-two opt">option two</p>
    <p class="option-three opt">option three</p>
    <p class="option-four opt">option four</p>

    <button id="option-one">one</button>
    <button id="option-two">two</button>
    <button id="option-three">three</button>
    <button id="option-four">four</button>

    <p class="option-one">option one</p>
    <p class="option-two opt">option two</p>
    <p class="option-three opt">option three</p>
    <p class="option-four opt">option four</p>

    $(document).ready(function(){
     $(".opt").hide();
     $("button").click(function(){
       $("p").hide();
       class_nmae = $(this).attr('id');
       $("."+class_nmae).show();
     });
   }); 

Have a common class/selector on all options to hide all options.

Have options specific classes to show then on demand.

have a common class/selector on buttons to add event listeners (click in this case).

This way, no matter how many options you add, they still work.

var buttons = document.querySelectorAll('.toggle-btn');
buttons.forEach(function(btn) {
  btn.addEventListener('click', function(evt) {
    var button = evt.target;
    var btnOption = button.getAttribute('data-target');
    hideAllOptions();
    showOption(btnOption);
  });
});

function hideAllOptions() {
  document.querySelectorAll('.options').forEach(function(option) {
    option.style.display = 'none';
  });
}

function showOption(target) {
  document.querySelectorAll(target).forEach(function(option) {
    option.style.display = 'block';
  })
}

// Show first option on load
buttons[0].click();
<p class="options option1">option one</p>
<p class="options option2">option two</p>
<p class="options option3">option three</p>
<p class="options option4">option four</p>

<button class="toggle-btn" data-target=".option1">one</button>
<button class="toggle-btn" data-target=".option2">two</button>
<button class="toggle-btn" data-target=".option3">three</button>
<button class="toggle-btn" data-target=".option4">four</button>

<p class="options option1">option one</p>
<p class="options option2">option two</p>
<p class="options option3">option three</p>
<p class="options option4">option four</p>

A possible solution in vanilla JavaScript that does not require you to change your html:

let topTexts = document.querySelectorAll('p[class$="top"');
let bottomTexts = document.querySelectorAll('p[class$="below"');
let buttons = document.querySelectorAll("button");

for(let i = 0; i < buttons.length; i++) {
  buttons[i].onclick = () => {
    Array.from(topTexts).map((e, j) => (e.hidden = i !== j));
    Array.from(bottomTexts).map((e, j) => (e.hidden = i !== j));
  };
}

buttons[0].click();
<p class="option1-top">option one</p>
<p class="option2-top">option two</p>
<p class="option3-top">option three</p>

<button id="option-one">one</button>
<button id="option-two">two</button>
<button id="option-three">three</button>

<p class="option1-below">option one</p>
<p class="option2-below">option two</p>
<p class="option3-below">option three</p>

Thank you all for the great solutions!

I ended up using @Neeraj Amoli solution and refined as follows:

  <div class="upper-timeline">
   <p class="option-one" style="opacity: 0;">option one</p>
   <p class="option-two" style="opacity: 0;">option two</p>
   <p class="option-three" style="opacity: 0;">option three</p>
   <p class="option-four" style="opacity: 0;">option four</p>
  </div>
  <div class="buttons">
    <button id="option-one">one</button>
    <button id="option-two">two</button>
    <button id="option-three">three</button>
    <button id="option-four">four</button>
  </div>
  <div class="lower-timeline">
   <p class="option-one" style="opacity: 0;">option one</p>
   <p class="option-two" style="opacity: 0;">option two</p>
   <p class="option-three" style="opacity: 0;">option three</p>
   <p class="option-four" style="opacity: 0;">option four</p>
  </div>
    $(document).ready(function(){
     $("p").addClass("remove");
     $(".option-one").addClass("show");
     $("button").hover(function(){
       $("p").removeClass("show").addClass("remove");
       class_nmae = $(this).attr('id');
       $("."+class_nmae).removeClass("remove").addClass("show");
     });
   }); 

jsfiddle

Related