Can you display a Div Section by using a Toggle button?

function toggleSections(section1, section2, section3, section4) {
  document.getElementById(section1).style.display = "block";
  document.getElementById(section2).style.display = "none";
  document.getElementById(section3).style.display = "none";
  document.getElementById(section4).style.display = "none";
}

function changeButtonStyle(btn1, btn2, btn3, btn4) {

  document.getElementById(btn1).className = "btn btn-sm btn-block bluebg noround padd10";
  document.getElementById(btn2).className = "btn btn-block btn-sm greyback noround padd10";
  document.getElementById(btn3).className = "btn btn-block btn-sm greyback noround padd10";
  document.getElementById(btn4).className = "btn btn-block btn-sm greyback noround padd10";
}
.greyback {
  background-color: rgb(240, 240, 240) !important;
  color: #000000;
}

.bluebg {
  background-color: rgba(26, 167, 227, 1.00) !important;
  color: #FFFFFF !important;
}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" />
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<div class="col-12">
  <div class="row">
    <div class="col-3">
      <button id="section1Btn" class="btn btn-block btn-sm lbbg" onclick="toggleSections('opt1','opt2','opt3','opt4'), changeButtonStyle('section1Btn','section2Btn','section3Btn','section4Btn')"> View</button></div>

    <div class="col-3"><button id="section2Btn" class="btn btn-block btn-sm greyback" onclick="toggleSections('opt2','opt1','opt3','opt4'), changeButtonStyle('section2Btn','section1Btn','section3Btn','section4Btn')">Add</button></div>

    <div class="col-3"> <button id="section3Btn" class="btn btn-block btn-sm greyback" onclick="toggleSections('opt3','opt1','opt2','opt4'), changeButtonStyle('section3Btn','section1Btn','section2Btn','section4Btn')">Modify</button></div>

    <div class="col-3"> <button id="section4Btn" class="btn btn-block btn-sm greyback" onclick="toggleSections('opt4','opt1','opt2','opt3'), changeButtonStyle('section4Btn','section1Btn','section2Btn','section3Btn')">Delete</button></div>


  </div>
</div>

<div id="opt1">Raj</div>
<div id="opt2">Ashok</div>
<div id="opt3">Manish</div>
<div id="opt4">Trivend</div>

I am experimenting with a JavaScript toggle option.

  1. The initial state has the first button active (with blue background).
  2. Initially, all hidden div sections are visible. The names should only appear when their corresponding button is clicked.
  3. The last button is not functioning properly - neither changing color nor opening the fourth section.

I am learning how to use Bootstrap classes and customized button backgrounds.

Any assistance would be greatly appreciated.

Answer №1

Exploring Point 1

The issue with your first button is that it has the lbbg class added, causing it to have a blue background and appear active. Removing this class and adding the greyback class will make it visible like the other buttons.

Investigating Point 2

Since you haven't specified a starting style for opt1, opt2, opt3, and opt4, they are visible by default. You can set their initial styles to display: none using CSS to hide them initially.

Diving into Point 3

In your code snippet:

onclick="showhidetog4('opt4',opt1','opt2','opt3'), optbttog3('ut4',ut1','ut2','ut3')"

you need to add a single quote before opt1 and ut1.

The corrected version should look like this:
onclick="showhidetog4('opt4','opt1','opt2','opt3'), optbttog3('ut4','ut1','ut2','ut3')"

I have provided the accurate code segment for your reference.

function showhidetog4(a, b, c, d) {
  document.getElementById(a).style.display = "block";
  document.getElementById(b).style.display = "none";
  document.getElementById(c).style.display = "none";
  document.getElementById(d).style.display = "none";
}

function optbttog3(a, b, c, d) {
  document.getElementById(a).className = "btn btn-sm btn-block lbbg noround padd10";
  document.getElementById(b).className = "btn btn-block btn-sm greyback noround padd10";
  document.getElementById(c).className = "btn btn-block btn-sm greyback noround padd10";
  document.getElementById(d).className = "btn btn-block btn-sm greyback noround padd10";
}
.greyback {
  background-color: rgb(240, 240, 240) !important;
  color: #000000;
}

.lbbg {
  background-color: rgba(26, 167, 227, 1.00) !important;
  color: #FFFFFF !important;
}

#opt1 {
  display: none;
}

#opt2 {
  display: none;
}

#opt3 {
  display: none;
}

#opt4 {
  display: none;
}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" />
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<div class="col-12">
  <div class="row">
    <div class="col-3">
      <button id="ut1" class="btn btn-block btn-sm greyback" onclick="showhidetog4('opt1','opt2','opt3','opt4'), optbttog3('ut1','ut2','ut3','ut4')"> View</button>
    </div>

    <div class="col-3">
      <button id="ut2" class="btn btn-block btn-sm greyback" onclick="showhidetog4('opt2','opt1','opt3','opt4'), optbttog3('ut2','ut1','ut3','ut4')">Add</button>
    </div>

    <div class="col-3">
      <button id="ut3" class="btn btn-block btn-sm  greyback " onclick="showhidetog4('opt3','opt1','opt2','opt4'), optbttog3('ut3','ut1','ut2','ut4')">Modify</button>
    </div>

    <div class="col-3">
      <button id="ut4" class="btn btn-block btn-sm  greyback" onclick="showhidetog4('opt4','opt1','opt2','opt3'), optbttog3('ut4','ut1','ut2','ut3')">Delete</button>
    </div>
  </div>
</div>

<div id="opt1">Raj</div>
<div id="opt2">Ashok</div>
<div id="opt3">Manish</div>
<div id="opt4">Trivend</div>

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

"Confusion arises when handling undefined arguments within async.apply in the context of async

While working on my project, I encountered a strange issue with the async library. Some of my arguments end up being "undefined" in my function calls. For example (this is just simplifying my problem): var token; async.series([ function getToken (do ...

The HTML checkbox remains unchanged even after the form is submitted

On a button click, I have a form that shows and hides when the close button is clicked. Inside the form, there is an HTML checkbox. When I check the checkbox, then close the form and reopen it by clicking the button again, the checkbox remains checked, whi ...

Struggling to align a vertical form horizontally in Bootstrap 4?

This code snippet shows the form I am working with: <div id="site" class="container-fluid"> <div class="row"> <div class="my-4 offset-3 col-6 justify-content-center align-items-center"> <form action="/some/path" method="po ...

The event handler for clicking on the inner <li> element is not being triggered

I am encountering an issue in my React project, The problem lies within a menu structure that contains nested sub-menus. Here is the snippet of code: <ul id="main-menu-navigation" data-menu="menu-navigation" ...

exploring the ins and outs of creating computed properties in TypeScript

How can I store an object with a dynamically assigned property name in an array, but unsure of how to define the array properly? class Driver { public id: string; public name: string; constructor(id , name) { this.id = id; th ...

Why doesn't the let variable used in a for loop initialization extend its scope to the enclosing block?

I've always been puzzled by this question: If block scopes are generated when a let or const identifier is enclosed within curly brackets, then how come the let identifier in the initialization statement of a for loop isn't accessible in the oute ...

"Converting circular structure into JSON" - Inserting BigQuery Data using Cloud Function in Node.js

I am currently facing an issue while attempting to load an array of JSON objects into a BigQuery Table from a Cloud Function built in NodeJS. Despite not having any circular references, I encountered the error message "Converting circular structure to JSON ...

I am experiencing issues with my React implementation of the bootstrap carousel

My carousel seems to be having some issues and I'm not sure what's causing it. Can someone provide guidance on how to resolve this problem? Here is the link to my code on Codesandbox: https://codesandbox.io/s/nice-heyrovsky-8yucf?file=/src/Prompt ...

Exploring VueJS reactivity: Updating an array with new data

I am struggling to understand why certain methods of changing data seem to work while others do not. In an attempt to clarify, I experimented with the following example: watch: { '$store.state.linedata': function() {this.redraw()} } ...

Display chosen preferences in an Angularjs dropdown alongside additional options

I'm currently developing a blogging software and have implemented an AngularJS dropdown for selecting post terms. Here's the code: <select multiple="multiple" name="terms" ng-model="post.data.attributes.term_ids" required> ...

Contentful integrated into a NuxtJs website

After successfully creating a blog using Nuxt JS and integrating Contentful into the project, I followed a helpful tutorial from here. This enabled me to retrieve all sections such as title, date, and content of the post efficiently. However, I am current ...

A method to incorporate the profile_pic attribute from a different model into a single model

I need to display the profile picture in the Blog View page that is stored in the Profile model models.py class Profile(models.Model): user = models.OneToOneField(User,null=True, on_delete=models.CASCADE) bio = models.TextField() profile_pic = ...

Storing an array in a cookie using Angular 8

Currently, I am utilizing the ngx-cookie-service package. { "user": [ { "id": 109, "name": "name1", "job" : "job name" } ], "token":"xxxx" } this.cookieService.set( 'user_id', ...

Choose three different images and one corresponding word from a JavaScript array to be displayed individually on the screen in separate div containers

I am in the process of developing a web game that will showcase 3 images on the screen, and the player must select the image that corresponds to the displayed word. I have successfully created a JavaScript array containing the images and words retrieved fr ...

Tips for effectively displaying camera.position.set (x,y,z) and camera.lookAt (x,y,z) within a three.js environment

While going through the tutorial on drawing lines in three.js documentation, I came across this code snippet: The code seems to be perfectly fine without any issues. <!DOCTYPE html> <html> <head> <meta charset="utf-8& ...

Not all properties are affected by the CSS stylesheet

Even though my CSS stylesheet successfully changes the background color of the page, I am facing issues with other properties that I have set on my HTML tags. {% block body %} <div id='title'> <h1> VISUALIZER2D ...

Animate the React Bootstrap modal only when it is shown or hidden

I am experimenting with the idea of transitioning smoothly from one modal to another in bootstrap Modals for react (using react-bootstrap). However, I am facing challenges due to the fade CSS class causing flickers and unwanted animations. My goal is to h ...

Tips for Presenting Json Information in Bootstrap Columns

I am working on a React App that showcases Json Data on a webpage Displayed below is the JSON file: [ { "name":"Apple", "price":"3,99", "description":"Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod te ...

Ts2532, The existence of the object is potentially unsafe

I am encountering an issue while trying to update a task in my project built with the MEAN stack. Although all APIs are functioning properly, I am facing an error when attempting to patch an element using the ID parameter. The error message displayed is: & ...

Uncovering the source of glitchy Angular Animations - could it be caused by CSS, code, or ng-directives? Plus, a bonus XKCD comic for some light-hearted humor

Just finished creating an XKCD app for a MEAN stack class I'm enrolled in, and I'm almost done with it. However, there's this annoying visual bug that's bothering me, especially with the angular animations. Here is the link to my deploy ...