How to toggle between two buttons

Viewed 698

here is the codepin: https://jsfiddle.net/magicschoolbusdropout/dwbmo3aj/18/

Currently, my code allows me to click on one button, content opens, click on the same button the content closes. Then I click on the second button, content opens, click on the same button content closes. I can switch from the second button to the first button, but then the content appends.

What I am trying to do is: toggle between the two buttons. Click on the first button, then the second button the info doesn't append or shift around. Would appreciate and and all help !! thanks :)

These are the functions im trying to toggle between:

function currentOrder(){
  var current = document.getElementById("current");
  if (current.style.display === "none") {
    current.style.display = "block";  
  } else {
    current.style.display = "none";
  }
}

function orderHistory(){
  var history = document.getElementById("history");
  if (history.style.display === "none") {
    history.style.display = "block";
  } else {
    history.style.display = "none";
  }
}

<<html>

<head>
  <script src="https://static.freshdev.io/fdk/2.0/assets/fresh_client.js"></script>
  <script src="https://code.jquery.com/jquery-3.6.0.min.js"
    integrity="sha256-/xUj+3OJU5yExlq6GSYGSHk7tPXikynS7ogEvDej/m4=" crossorigin="anonymous"></script>
  <link rel="stylesheet" type="text/css" href="style.css" />
  <link rel="stylesheet" type="text/css" href="https://static.freshdev.io/fdk/2.0/assets/freshdesk.css" />
</head>

<body>
<button onclick="currentOrder()">Current Order</button>
<button onclick="orderHistory()">Order History</button>

<div id ="current">
  <div class="">
    <div class="item1-container">
      <div class="container">
        <br>
        <h5 class="text"><span class="key"> Order #: </span><span class="value">121800</span></h5>
        <h5 class="text line"><span class="key"> Name: </span><span id="name" class="value"></span></h5>
        <h5 class="text"><span class="key"> Status Update : </span><span class="value">Pending</span></h5>
        <br>
        <h4 class="text"><span class="key"> <b> Non-Dairy Blue Matcha </b></span></h4>
        <img src="matchadrink.png" ALIGN=left alt="item1 image" class="image">
        <div class="container_text" id="item1_details">
          <p class="text"><span class="key">Price: </span><span class="value">$</span><span class="value"
              id="item-price" style="margin-left:auto">4.44</span></p>
          <p class="text"><span class="key">Quantity: </span><span id="quantity" class="value">0</span></p>
          <div class="quantity-wrapper">
            <div class="value-button" id="increase" onclick="increaseValue()" value="Increase Value"></div>
            <input type="number" id="number" value="0" />
            <div class="value-button" id="decrease" onclick="decreaseValue()" value="Decrease Value"></div>
            <input type="submit" name="save" value="save" id="add_quantity">
          </div>
        </div>
      </div>
    </div>
  </div><br>
  <div class="">
    <div class="item2-container">
      <div class="container">
        <h4 class="text"><span class="key"> <b>S'mores FroYo</b></span></h4>
        <img src="smores.png" ALIGN=left alt="item2 image" class="image">
        <div class="container_text" id="item1_details">
          <p class="text"><span class="key">Price: </span><span class="value">$</span><span class="value"
              id="item-price">5.20</span></p>
          <p class="text"><span class="key">Quantity: </span><span id="quantity1" class="value">0</span></p>
          <div class="quantity-wrapper">
            <div class="value-button" id="decrease" onclick="decreaseValue()" value="Decrease Value"></div>
            <input type="number1" id="number1" value="0" />
            <div class="value-button" id="increase" onclick="increaseValue()" value="Increase Value"></div>
            <input type="submit" name="save" value="save" id="add_quantity1">
          </div>
        </div>
      </div>
    </div>
  </div>
</div>

<div id="history">
  <div class="">
 <h3> Order History</h3> <br>
 <p>
  <h5 class="text"><span class="key"> Order #: </span><span class="value">123456</span></h5>
  <h5 class="text"><span class="key"> Name: </span><span id="name" class="value"></span></h5>
  <h5 class="text line"><span class="key"> Status Update : </span><span class="value">Completed</span></h5>
 </p>
 <p>
  <h5 class="text"><span class="key"> Order #: </span><span class="value">56789</span></h5>
  <h5 class="text"><span class="key"> Name: </span><span id="name" class="value"></span></h5>
  <h5 class="text line"><span class="key"> Status Update : </span><span class="value">Completed</span></h5>
 </p>
 <p>
  <h5 class="text"><span class="key"> Order #: </span><span class="value">10987</span></h5>
  <h5 class="text"><span class="key"> Name: </span><span id="name" class="value"></span></h5>
  <h5 class="text line"><span class="key"> Status Update : </span><span class="value">Completed</span></h5>
 </p>
 <p>
  <h5 class="text"><span class="key"> Order #: </span><span class="value">082402</span></h5>
  <h5 class="text"><span class="key"> Name: </span><span id="name" class="value"></span></h5>
  <h5 class="text line"><span class="key"> Status Update : </span><span class="value">Completed</span></h5>
 </p>
 <p>
  <h5 class="text"><span class="key"> Order #: </span><span class="value">081893</span></h5>
  <h5 class="text"><span class="key"> Name: </span><span id="name" class="value"></span></h5>
  <h5 class="text line"><span class="key"> Status Update : </span><span class="value">Completed</span></h5>
 </p>
  </div>
</div>
</body>
<script src="app.js"></script>

</html>>
1 Answers

Just move the assignments outside of the functions.

This should work:

let current = document.getElementById("current");
let history = document.getElementById("history");

function currentOrder(){
    current.style.display = "block";
    history.style.display = "none"
}

function orderHistory(){
    current.style.display = "none";
    history.style.display = "block"
}

You don't need to check current state and just switch the styles. Keep it simple ... (kiss).

Related