Navigating through different perspectives using JQuery

I currently have a sidebar menu that controls the display of content in the main section.

While I've successfully implemented it using JQuery, I can't help but feel like I'm repeating myself too much within the code.

Is there a more efficient way to achieve this functionality, making it more scalable and easier to add new elements without constantly updating the JQuery code?

If you'd like to take a look at my project, here is the link to the fiddle: https://jsfiddle.net/danjodesigns/9r20sfmb/3/

I would greatly appreciate any suggestions or improvements you may have.

JQuery:

$(".settings-btn-1").click(function () {
    $('.settings-menu-btn').removeClass("active");
    $(this).addClass("active");
    $('.settings-view').removeClass("active");
    $('.view-1').addClass("active");
});
$(".settings-btn-2").click(function () {
    $('.settings-menu-btn').removeClass("active");
    $(this).addClass("active");
    $('.settings-view').removeClass("active");
    $('.view-2').addClass("active");
});
$(".settings-btn-3").click(function () {
    $('.settings-menu-btn').removeClass("active");
    $(this).addClass("active");
    $('.settings-view').removeClass("active");
    $('.view-3').addClass("active");
});

HTML:

    <div class="container">

    <div class="settings-sidebar">

        <ul class="settings-menu">
           <li class="settings-menu-btn settings-btn-1 active"><span>Account Details</span></li>
           <li class="settings-menu-btn settings-btn-2"><span>Profile Information</span></li>
           <li class="settings-menu-btn settings-btn-3"><span>Categories</span></li>
        </ul>
    </div>

    <div class="settings-view-placeholder">

        <section class="settings-view view-1 active">
            <h2>Account Details</h2>
            <p>Review and update your account details.</p>
        </section>

        <section class="settings-view view-2">
            <h2>Profile Information</h2>
            <p>View your profile information.</p>
        </section>

        <section class="settings-view view-3">
            <h2>Categories</h2>
            <p>Review and update your store categories. These tags will determine what shoppers your store is suggested to.</p>
        </section>


    </div>

</div>

CSS:

.container {
  display:flex;
  flex-direction:row;
  width:100%;
}

.settings-sidebar {
  background-color:white;
  margin-right:15px;
}

.settings-menu {
  list-style:none;
  padding:0;
  margin:0;
  width:250px;
}

.settings-menu-btn {
  border-bottom:solid 1px #EEEEEE;
  padding:15px;
  cursor: pointer;
}

.settings-menu-btn.active {
  background-color:#191919 !important;
  color:#FFFFFF !important;
}

.settings-menu-btn:hover {
  background-color:#F7F7F7;
}

.settings-view-placeholder {
  background-color:#FFFFFF;
  padding:30px;
  flex-grow:1;
}

.settings-view {
    display: none;
    flex-direction:column;
}

.view-1.active,
.view-2.active,
.view-3.active {
    display: flex !important;
}

Answer №1

Avoid using numeric classes ... Instead of li with class settings-menu-btn, you can utilize .index(). Also, there is no need for individual classes like view-1, view-2...; simply use settings-view and .eq() directly.

$("li.settings-menu-btn").click(function () {
    var ThisIndex = $(this).index();
    $('.settings-menu-btn').removeClass("active");
    $(this).addClass("active");
    $('.settings-view').removeClass("active");
    $('.settings-view').eq(ThisIndex).addClass("active");
});
.container {
  display:flex;
  flex-direction:row;
  width:100%;
}

.settings-sidebar {
  background-color:white;
  margin-right:15px;
}

.settings-menu {
  list-style:none;
  padding:0;
  margin:0;
  width:250px;
}

.settings-menu-btn {
  border-bottom:solid 1px #EEEEEE;
  padding:15px;
  cursor: pointer;
}

.settings-menu-btn.active {
  background-color:#191919 !important;
  color:#FFFFFF !important;
}

.settings-menu-btn:hover {
  background-color:#F7F7F7;
}

.settings-view-placeholder {
  background-color:#FFFFFF;
  padding:30px;
  flex-grow:1;
}

.settings-view {
    display: none;
    flex-direction:column;
}

.settings-view.active {
    display: flex !important;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="container">

    <div class="settings-sidebar">

        <ul class="settings-menu">
           <li class="settings-menu-btn active"><span>Account Details</span></li>
           <li class="settings-menu-btn"><span>Profile Information</span></li>
           <li class="settings-menu-btn"><span>Categories</span></li>
        </ul>
    </div>

    <div class="settings-view-placeholder">

        <section class="settings-view active">
            <h2>Account Details</h2>
            <p>Review and update your account details.</p>
        </section>

        <section class="settings-view">
            <h2>Profile Information</h2>
            <p>View your profile information.</p>
        </section>

        <section class="settings-view">
            <h2>Categories</h2>
            <p>Review and update your store categories. These tags will determine what shoppers your store is suggested to.</p>
        </section>


    </div>

</div>

and instead of using view-1, view-2, ..., make the change:

.settings-view.active {
    display: flex !important;
}

Answer №2

To enhance your code, consider implementing the following changes:

$('.sidebar-settings').on('click', '.menu-btn:not(.active)', function() {
  $('.view-settings').eq($(this).index()).add(this).add('.active').toggleClass("active");
});

-Link to jsFiddle-

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

The CSS border-image feature seems to be malfunctioning on Opera browser

I've been trying to implement border images in my application using CSS, but unfortunately, it doesn't seem to be working in Opera. Does anyone know how I can get Opera to support this feature? ...

Prevent the top of the fifth row from displaying on mobile devices when the first four rows are hidden

My personal website features a collection of user-generated stories, with only the first 4 rows of each story visible in a fading text gradient from black to white. There is a "show more/less" button to reveal or hide the full content. While this layout ...

Show the div only if a different ID is activated

I seem to be having trouble targeting only one div element. Specifically, when I click on "impressum," only the impressum content is displayed within the <div class="ContentBox">. However, when I click on "AboutMe," both the AboutMe and impressum co ...

Tips for accelerating an HTML element to move into position

I am working on a website where I have an element that remains in the upper corner and follows the user as they scroll down. You can see an example of this behavior in this Fiddle. However, I want to enhance this effect by allowing the element to smoothl ...

Ways to prevent the checked value from being continually added to the input text box

I am currently working on a project that involves integrating radio button values with an input field. The purpose of the input field is to enter a name, while the radio buttons are meant for selecting the gender. The default text in the input field shoul ...

Adjust the size and position of the text input field

I'm struggling to find a way to both resize and move the box without one action interfering with the other. Whenever I attempt to change the size of the box, it triggers the move event at the same time. Clicking on the box should make it resizable, bu ...

Using Bootstrap to create a fixed footer in Laravel

I have a Laravel project with Bootstrap added as the CSS. I have tried everything to make my sticky footer work, but when the page is longer than the window, it remains at the bottom of the window when scrolled up. Here is my main: <html> <head& ...

Ways to streamline repetitive CSS rules within media queries

As I work on defining multiple media queries, I realize there is some redundant code within them: @media only screen and (max-width:768px), only screen and (min-width:769px) and (max-width:1040px) { .list__figure { margin-right: 20px; ...

methods for modifying multiple elements with getElementById

I am facing an issue where only the first getElementById code is working and changing the value of the ID. Here is my HTML: <li ><a href="#Home" id="activehome" onclick="load_home()">HOME</a></li> <li ><a href="#Products" ...

Slickgrid's scrollRowIntoView function isn't behaving as anticipated

Here is my code that utilizes the scrollRowIntoView function. I am making use of the resizer to adjust the grid's length to match the browser window. Currently, I have hardcoded a number as an example, but eventually, I will fetch the record to be scr ...

Issue with the progress bar bouncing back and forth

I am encountering an issue with my progress bar width changing on nav-links click. When I click on each nav-link, the progress bar reaches its width correctly but then turns back and forth unexpectedly. I am unsure why this is happening. Here is the code ...

Swapping out an external CSS library file for a locally-saved alternative

Currently, I am importing a CSS library directly into my main CSS file like this: @import url(https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.8.2/css/all.min.css); However, I would like to store it locally in my project and import it from its folde ...

table with flexible cell width and fixed table width

I've been troubleshooting this issue for days, but I just can't seem to find a solution! The problem lies within a table used on a webpage. If I don't specify table-layout, I can make one cell width dynamic, but I lose the ability to set &a ...

Ways to solve the padding issue in Bootstrap 5

I am encountering an issue with setting the padding in my fluid container. The top-down padding is set to 3%, but the left-right padding of 15% is not working as expected. After debugging using Chrome Developer Tools, I noticed that the left-right padding ...

Creating a Unique Carousel Design with Ant Design

https://i.sstatic.net/HobMm.jpg Steps to personalize the ant design carousel with these unique features: Use Bullets instead of Boxes Modify Bullet Color Place Bullets outside the image container Sandbox Link https://codesandbox.io/s/trusting-dream-zzjr ...

Is there a way to customize CKEditor to prevent it from continuously adding <p></p> tags within the textarea automatically?

As I was going through the CKEditor tutorial, I implemented the following: $( '#editor' ).ckeditor(function(){}); //it's working great!! However, after submitting the form, I noticed that by default, the textarea displays <p></p ...

Ways to retrieve a specific value from a JSON dataset

I currently have an Ajax live search script with multiple text inputs for searching products. The live search functionality works perfectly, but I have a query: If I decide to change the name of a product, how can I go back to the previous page and re-s ...

Understanding the functionality of $q in AngularJS

Can someone help explain how $q functions in Angular? Let's say I have a few lines of code that need to be executed. var app = angular.module('app', []); app.controller('HelloCtrl', function ($q, $scope) { $scope.Title = "Pr ...

Is it possible to include edit links to local files in an HTML report that I am creating?

Looking to enhance the readability of py.test reports that look like this: self = <test_testcenter_views.TestSiteViews testMethod=test_testcenter> def test_testcenter(self): "Test the tctr_update view." > r = self.client.get(&a ...

jQuery fails to generate correct HTML code

I am currently working on maintaining an older Rails 3 project that I need to update with new features, particularly adding responsiveness using Bootstrap 4. The end result I am aiming for can be viewed here: http://jsfiddle.net/k1zbe4o9/. It functions pe ...