Dynamic drop-down navigation with rearranging menu

I am attempting to design a drop-down navigation bar with 2 rows. Initially, only 1 row is visible. Upon clicking the visible row, both rows should appear. Subsequently, when one of the 2 rows is clicked, only that specific row should be displayed. Below are a few images illustrating my desired outcome.

This example showcases what I am aiming for, however, I would like to accomplish it without utilizing jQuery. https://codepen.io/matthewbeta/pen/zioHr

https://i.sstatic.net/tYVqk.jpg https://i.sstatic.net/Jhsvv.jpg https://i.sstatic.net/g4Nqd.jpg

It is partially functional already, yet I am struggling to display Row-B only upon its click. Currently, clicking on Row-A reveals both rows simultaneously. Conversely, selecting Row-B causes Row-A to be the sole visible row.

var select = document.getElementById("select");
var rowB = document.getElementById("rowB");
rowB.style.display = "none";

select.addEventListener("click", function(){
  if (rowB.style.display == "none") {
    rowB.style.display = "flex";
  }
  else {rowB.style.display = "none"};
});
body {
  margin: 0px;
}

#content {
  width: 300px;
  height: 300px;
  background: #000;
  display: grid;
  grid-template-rows: 70px 160px 70px;
}

#select {
  width: 270px;
  height: 90px;
  margin: 0 auto;
  color: #fff;
  margin-top: 10px;
  display: flex;
  flex-direction: column;
  justify-content: flex-start;
  align-items: flex-start;
  z-index: 2;
}

#rowA,
#rowB {
  width: 270px;
  height: 40px;
  background: #000;
  border: #2e2e2e 1px solid;
  display: flex;
  align-items: center;
  z-index: 2;
}
<body>
  <div id="content">
    <div id="select">
      <div id="rowA">Row-A</div>
      <div id="rowB">Row-B</div>
    </div>
  </div>
</body>

Answer №1

Here are some tweaks to the JavaScript portion:

var selection = document.getElementById("selection");
var rowC = document.getElementById("rowC");
rowB.style.display = "none";

selection.addEventListener("click", function(){
  if(event.target.id === 'rowA') {
    rowB.style.display = (rowB.style.display === 'none')? "block" : "none";
    rowB.style.order = '1';
    rowA.style.order = '0';
  }
  else if(event.target.id === 'rowB') {
    rowA.style.display = (rowA.style.display === 'none')? "block" : "none";
    rowA.style.order = '1';
    rowB.style.order = '0';
  }
});
body {
  margin: 0px;
}

#content {
  width: 300px;
  height: 300px;
  background: #000;
  display: grid;
  grid-template-rows: 70px 160px 70px;
}

#selection {
  width: 270px;
  height: 90px;
  margin: 0 auto;
  color: #fff;
  margin-top: 10px;
  display: flex;
  flex-direction: column;
  justify-content: flex-start;
  align-items: flex-start;
  z-index: 2;
}

#rowA,
#rowB {
  width: 270px;
  height: 40px;
  background: #000;
  border: #2e2e2e 1px solid;
  display: flex;
  align-items: center;
  z-index: 2;
}
<body>
  <div id="content">
    <div id="selection">
      <div id="rowA">Row-A</div>
      <div id="rowB">Row-B</div>
    </div>
  </div>
</body>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Establishing a Goal in Google Analytics using JavaScript

My approach to recording Goals in Google Analytic tool via JS involves adding code directly to the page: <meta name="description" content="iReserve"> <link rel="shortcut icon" href="/ico/favicon.ico" /> & ...

Setting up a React Package by reinstalling a git repository

While immersing myself in learning React JS, I decided to create a git repository containing React components that could be exported and used or installed as a package in a separate React application. I developed some UI Components in a git repository and ...

Utilize AngularJS's nested ng-repeat to showcase information from two distinct JSON strings simultaneously

I am dealing with two different JSON strings from databases. [{ sport: football, sportid: 1 },{ sport: tennis, sportid: 2 },{ sport: golf, sportid: 3 },{ sport: swimming, sportid: 4 }] and [{ personid: 1, name: ...

Accessing feedback from Reddit's API

After writing some code to search Reddit's API with a specific query, I now want it to display comments as well. Inside my $.getJSON statement that retrieves each title/post based on the search query, I have the following nested code block. The goal i ...

Explanation on subtracting the row value from a textbox

Click here to view the image For instance: If I enter a value in the "USED(kl)" textbox, it should subtract that value from the corresponding row where "KILOGRAM/S" is located; Upon clicking the update button, only the specific row should be affected wh ...

When an iteration occurs within a for loop and a value changes, remember to incorporate a line break or equivalent element using jQuery

I am currently working on implementing a hierarchy system for users stored in a database table. At the moment, only top-level users with a hierarchy of 1 are displayed. When clicked, I use AJAX to retrieve and display all related users below them. These ...

Issue with Font-Awesome icons failing to display properly when using the BootstrapCDN

Trying to integrate Font-Awesome icon fonts using the BootstrapCDN link with what seems like the latest version: <link href="//netdna.bootstrapcdn.com/font-awesome/4.0.3/css/font-awesome.css" rel="stylesheet"> The link has been added to the <hea ...

What is the best way to guide a user to a specific page based on the choice they made after submitting a form?

My form includes a list of 6 options where users can only select one. After submitting the form, I want to redirect them to a specific page based on their selection. For example, I have the following 3 options: Facebook Youtube Twitter If a user selects ...

Is foreach not iterating through the elements properly?

In my code, I have a loop on rxDetails that is supposed to add a new field payAmount if any rxNumber matches with the data. However, when I run the forEach loop as shown below, it always misses the rxNumber 15131503 in the return. I'm not sure what I ...

Encountering a JavaScript runtime error while trying to access and interpret JSON

Currently, I'm facing a challenge with converting a C# list of string types into a JSON object. The issue arises when trying to read this JSON object later in JavaScript. On the other hand, the process seems to work fine when dealing with a C# list of ...

GTM - monitoring the most recent clicked element's input data

Custom Script function() { var inputs = document.getElementsByTagName("input"), selectedRadios = []; for (var i = 0;i < inputs.length;i++) { if(inputs[i].type==="checkbox" && inputs[i].checked) { selectedRadios.push(inputs[i].value); ...

Need help figuring out how to use Javascript:void(0) to click a button in Selenium?

Struggling with finding the right solution, I have attempted various methods. Apologies for any formatting errors, but the element that requires clicking is: <button href="javascript:void(0)" id="payNewBeneficiary" class="button-new-payee"> ...

How can I click on an item when using pagination?

When using react.js, I am encountering a difficult-to-understand error. My component works fine without pagination - it displays all items and allows you to click on them. Pagination is working properly as well, but the issue arises when trying to click on ...

Will synchronous programming on an Express server prevent other users from accessing the page simultaneously?

Currently working on a simple one-page web app that depends on loading weather data from a file. To avoid multiple HTTP requests for each visitor, I have a separate script refreshing the data periodically. In this particular instance, I am using fs.readFi ...

Showing dynamic icons in Angular 2 applications

My goal is to dynamically load a part of my website, specifically by using icon classes defined in the interface like this: import { OpaqueToken } from "@angular/core"; import {IAppConfig} from './app.interface' export let APP_CONFIG = new Opaq ...

Struggling to align your website content properly?

Currently, I am attempting to center all of the products on my Wordpress page so that everything is aligned centrally. I attempted wrapping it all in a div with specific CSS properties, but unfortunately, the content moved to the middle while the products ...

Retrieve the country code of an IP address using getJSON

I am currently facing an unusual issue. My goal is to extract the country code (like US for United States) from an IP address using free APIs. After some research, I came across ipify for retrieving the IP address (which works fine), and then attempted to ...

Tips for enhancing the efficiency of my JavaScript code?

On my page, I have a list of items with various actions assigned to them. One can either click on an icon next to each item or check a checkbox on the left side. However, when clicking on an action after selecting multiple items, there is a noticeable lag ...

Display data in a template upon receiving a response from the server

Hello, I am currently in the process of developing my very first Angular application and could use some assistance. Specifically, I am working on a component that serves as an image search box. When a user inputs a search query, a request is sent to an API ...

What is the process for converting the output of cryptoJS.sha256 to binary in a Postman pre-request script?

Seeking assistance in creating an HMAC signature using a pre-request script in Postman. While troubleshooting, it has become apparent that there is an issue with the signature generation process. Although a proof of concept example provides expected result ...