Using ReactJS to toggle a div upon clicking

I am facing an issue with toggling a div by clicking on text within a toggle bar. I am in the process of recreating a WordPress website template in React JS that was originally built using bootstrap. You can view the original template here.

So far, I have successfully recreated all sections using simple HTML and CSS and rendered them using React's class method (although I plan to switch to function as there is no state involved). However, I have reached a section (the "get directions to event hall" section, second last) where there is a navigation bar that animates, switches the displayed data, and changes the active heading along with a small pointer beneath it. The image below illustrates this:

By default, the "Venue" heading is active. Clicking on either "Time" or "How to get there" should update the pink pointer position and change its size accordingly. Furthermore, clicking on any of these headings should animate the switch between the content underneath (even though currently, they show the same text).

To achieve this functionality while retaining the current active heading, here is the relevant code snippet:

import React,{Component} from 'react'

class Directions extends Component{
    render(){
        return(
            <section id = "Directions-section">
                <div id = "directions-div">
                    <div id="directions-text">
                        ...
                    </div>
              </section>
         )
        }

This code represents one instance of the div structure. I intend to duplicate this for each heading and apply the recommended approach.

The corresponding CSS:

#Directions-section
{
    ...
}

#directions-div
{
    ...
}

...

P.S: Bootstrap was omitted initially due to a misunderstanding, and integrating it later caused disruptions after months of effort!


Edit: Additionally, I require text changes and animations upon clicking different headings.

Answer №1

Make a simple change to your ul element

<ul>
            {["Location","Date","Directions"].map((item,index) => 
            <li onClick={() => this.setState({chosen: index})} 
          className={this.state.chosen === index ?"selected":null}>{item}</li>)}
         </ul>

In addition,

insert the following code above the render() method

state = { 
        chosen: 0
     }

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

Challenges in working with PHP, SQL, HTML, and AJAX data submission

Having encountered another issue, I've been attempting to make an AJAX script function properly, but whenever I click on it, the page reloads without updating the database field. The code I'm using has successfully worked for similar scripts on ...

The Embla Carousel feature is not compatible with next.js server-side rendering (SSR

My component is rendered server side (next.js). Embla Carousel does not work with server rendering. const [viewportRef, embla] = useEmblaCarousel( { slidesToScroll: "auto", axis: "y", skipSnaps: true, ...

Guide on how to forward the response obtained from an Ajax call to a different HTML page

I am working with a mongoose database that stores data containing an individual's first and last name. The user inputs their first name into a field, triggering an ajax request sent to the file named controller.js. This file generates a JSON response ...

Update the state in the componentDidMount lifecycle method

Embarking on a project using React to hone my skills, but encountered an error while trying to populate an array with data from a JSON file in the ComponentDidMount() hook. It seems this issue stemmed from a previous error: cannot read property 0 of undef ...

Encountering a problem with server-side jQuery autocomplete

I attempted to utilize a customized service I created in order to make this jquery autocomplete example work, but unfortunately it's not functioning properly. Here is the code snippet: $( "#city" ).autocomplete({ source: function( request, res ...

REST, hypertext, and clients other than web browsers

I'm struggling to understand how a REST API can be both hypertext driven, as explained in this article, and still be machine readable. Let's say I create an API where one endpoint lists the contents of a collection. GET /api/companies/ When the ...

Separate the text content stored in the database into individual paragraphs

I have a piece of text saved in my database : model.Description : "Bacon ipsum dolor sit amet fatback pork belly swine cow drumstick jowl" In my view, I would like to split the text like so: <p> Bacon ipsum dolor </p> <p> sit ...

Enhance the appearance of div elements in GWT/HTML with custom styling

I am relatively new to GWT and have been exploring various options, but I have not yet found a solution to my issue. The challenge at hand involves developing a function schedule system in GWT, where I am working on designing the frontend. We currently ha ...

Can anyone provide guidance on how to trigger an HTTP 500 error in my React.js and Next.js applications?

To ensure that our custom error page is displayed instead of the default HTTP 500 error following a recent security vulnerability, I am looking to purposely simulate the error. While we have special processing in place for 404 and 403 errors on our site, ...

I'm wondering how I can design a utility function within my Redux module that can extract a specific subset of read-only data from the current state

I am currently utilizing redux to create a "helper function" inside my redux module that is responsible for fetching filtered data from the state based on a specified index. This specific data will be used to generate a form consisting of inputs depending ...

CSS - Creating a Gradient Effect on div Borders for a Transparent Fade Effect across a Specified Distance

I am trying to achieve a fading effect for the background color of a div that contains a form. The solid background should fade out to transparent based on a pixel variable, allowing the background behind it to show through. This pixel value determines how ...

Fixed-positioned elements

I'm facing a small issue with HTML5 that I can't seem to figure out. Currently, I have a header image followed by a menu div containing a nav element directly below it. My goal is to make the menu div stay fixed when scrolling down while keeping ...

Which language should I focus on mastering in order to create a script for my Epson receipt printer? (Check out the manual for reference)

Printer Model: TM-T88V User Manual: Receipt-marker Web template for the printer: (Provided by Epson) I'm completely new to computer languages and have been trying to understand how to print receipts. I've researched XML, HTML, CSS, and Jav ...

When resizing the browser window, this single horizontal page website does not re-center

I am in the process of creating my very first one-page horizontal website using jQuery and the scrollTo plugin. This site consists of 3 screens and initially starts in the center (displaying the middle screen). To achieve this, I decided to set the width o ...

Troubleshooting Issue with Material Ui DataGrid IsRowSelectable Feature in React TypeScript

Currently, I am working on a project using React TSX and have implemented a Datagrid with checkbox selection enabled. However, I need to disable the selection for specific rows. After some research, I discovered that the isRowSelectable prop in the materia ...

Enhancing CSS compatibility for Internet Explorer 9 with CSS3

I need to create a Jagged Edge border without relying on an image, and I want it to be compatible with IE9 and newer versions. Currently, it works in most browsers but not in IE9. Any suggestions or advice would be greatly appreciated. Thank you! Below is ...

Tips on generating an HTML element using JavaScript and storing it in a MySQL database

I need help with saving a created element to the database so that it remains on the page even after refreshing. Any assistance would be greatly appreciated. Thank you. document.getElementById("insert").onclick = function(){ if(document.getElementById( ...

What is the best way to activate yup validation in react-hook-form prior to the user clicking the submit button?

I am currently using react-hook-form along with yup validation and MUI components in my react TypeScript application. The issue I am facing is that the errors from the yup validation only appear after the user tries to submit the form by clicking on the su ...

The lengthy string is not breaking into separate lines as expected in Internet Explorer

My querystring is quite long http://terra.cic.local/web/index.cfm//pm/uebersicht?sucheAufgeklappt=was%2Cwie%2Cwohin%2Cwann%2Cwer&sucheVon=&sucheBis=&sucheIstErsteSeiteAnzahlProdukteErmitteln=false&sucheIDReiseart=26&sucheHotelart=1081& ...

Manipulate your table data with jQuery: add, update, delete, and display information with ease

When adding data dynamically to the table and attempting to edit, the checkbox value is not updating. This issue needs to be resolved. You can view the code in action on jsFiddle here on JSFiddle ...