Tips on how to select only the currently active tab and change its background color

I am currently troubleshooting the AJAX tabs functionality on my website. The issue I am facing is that the current code does not apply any styling to indicate an active tab.

I believe I may need to use some JavaScript code to address this problem, but I am unsure where to begin.

Here is the HTML code snippet:

$("h2").addClass("white");
    $(document).ready(function () {
        // load index page when the page loads
        $("#response").html("DEFAULT");
        $("#page1").click(function () {
            // load home page on click
            $("#response").html("<h2> Welcome </h2><p>Sed porttitor lectus nibh. Cras ultricies ligula sed magna dictum porta. Praesent sapien massa, convallis a pellentesque nec, egestas non nisi. <br/> Curabitur arcu erat, accumsan id imperdiet et, porttitor at sem. Nulla quis lorem ut libero malesuada feugiat. Lorem ipsum dolor sit amet, consectetur adipiscing elit.</p>");
        });
        $("#page2").click(function () {
            // load about page on click
            $("#response").html("TESTING 2");
        });
        $("#page3").click(function () {
            // load contact form onclick
            $("#response").html("TESTING 3");
        });
        $("#page4").click(function () {
            // load contact form onclick
            $("#response").html("TESTING 4");
        });
        $("#page5").click(function () {
            // load contact form onclick
            $("#response").html("TESTING 5");
        });
    });
li {
    display: inline-block;
    margin-right: 2px;

}
li a {
   
    padding: 4px;
    color:#0086ca;
    text-decoration: none;
}

.white {
    color: green;
}

.contentarea {
    margin-top: 30px;
    width: 100%;
    padding: 40px 50px;
    background-color: #0086ca;
    color: white;
    text-align: left;
    font-size: 24px;
}

/* Code for multi-step indicator */
@media only screen and (min-width: 768px) {
  /* CSS for multi-steps indicator styles */
}

/* Counter for multi-steps indicator */
.cd-multi-steps.count li {
  counter-increment: steps;
}

/* Implementing active tab styling */
@media only screen and (min-width: 768px) {
  /* CSS rules to style the active tab */
}

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<nav>
    <ul class="cd-multi-steps text-bottom count">
        <li class="current"><a href="#tabs-1" id="page1" class="tab">page 1</a>

        </li>
        <li><a href="#tabs-2" id="page2" class="tab">page 2</a>

        </li>
        <li><a href="#tabs-3" id="page3" class="tab">page 3</a>

        </li>
        <li><a href="#tabs-4" id="page4" class="tab">page 4</a>

        </li>
        <li><a href="#tabs-5" id="page5" class="tab">page 5</a>

        </li>
    </ul>
</nav>
<div class="contentarea" id="response"></div>

I would like to know how can I differentiate the active tab from the rest?

Thank you.

Answer №1

There's no need to click for each individual id. I've streamlined your code and included .active on click for simplicity.

     $("h2").addClass("white");
     $(document).ready(function() {
       // Load index page upon initial page load
       
       var data = {
         '#tabs-1':'Testing 1',
         '#tabs-2':'Testing 2',
         '#tabs-3':'Testing 3',
         '#tabs-4':'Testing 4',
         '#tabs-5':'Testing 5'
       };
       
       $("#response").html("DEFAULT");
       
       $('.tab').click(function(){
         $('.tab').removeClass('active'); // Remove .active from all tabs
         $(this).addClass('active'); // Add .active to currently pressed tab
         href = $(this).attr('href');
         $("#response").html(data[href]);
       });

     });
li {
  display: inline-block;
  margin-right: 2px;
}
li a {
  padding: 8px 4px;
  color: #0086ca;
  text-decoration: none;
}


li a.active {
  color: white;
  background:#0086ca;
}

.contentarea {
  margin-top: 30px;
  width: 100%;
  padding: 40px 50px;
  background-color: #0086ca;
  color: white;
  text-align: left;
  font-size: 24px;
}
.cd-multi-steps.count li {
  counter-increment: steps;
}
.cd-multi-steps.count li > *::before {
  /*content: counter(steps)" - ";*/
}
@media only screen and (min-width: 768px) {
  .cd-multi-steps.text-top.count li > *::before,
  .cd-multi-steps.text-bottom.count li > *::before {
    /* This is the spot indicator 
    content: counter(steps); */
    height: 26px;
    width: 26px;
    line-height: 26px;
    font-size: 1.4rem;
    color: #ffffff;
  }
  .cd-multi-steps.text-top.count li:not(.current) em::before,
  .cd-multi-steps.text-bottom.count li:not(.current) em::before {
    /* Steps not visited yet - counter color */
    color: #2c3f4c;
  }
  .cd-multi-steps.text-top.count li::after {
    bottom: 11px;
  }
  .cd-multi-steps.text-top.count li > * {
    padding-bottom: 34px;
  }
  .cd-multi-steps.text-bottom.count li::after {
    top: 11px;
  }
  .cd-multi-steps.text-bottom.count li > * {
    padding-top: 34px;
  }
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<nav>
  <ul class="cd-multi-steps text-bottom count">
    <li class="current"><a href="#tabs-1" id="page1" class="tab">page 1</a>

    </li>
    <li class="current"><a href="#tabs-2" id="page2" class="tab">page 2</a>

    </li>
    <li><a href="#tabs-3" id="page3" class="tab">page 3</a>

    </li>
    <li><a href="#tabs-4" id="page4" class="tab">page 4</a>

    </li>
    <li><a href="#tabs-5" id="page5" class="tab">page 5</a>

    </li>
  </ul>
</nav>
<div class="contentarea" id="response"></div>

Answer №2

Here is some code that could be helpful:

Try using the addClass() function inside a click event

$("li").click(function(){
  $(this).addClass("active").siblings().removeClass('active');
})

$("#page1").click(function () {
 // Load the home page when clicked
 $("#response").html("<h2> Welcome </h2><p>Sed porttitor lectus nibh. Cras ultricies ligula sed magna dictum porta. Praesent sapien massa, convallis a pellentesque nec, egestas non nisi. <br/> Curabitur arcu erat, accumsan id imperdiet et, porttitor at sem. Nulla quis lorem ut libero malesuada feugiat. Lorem ipsum dolor sit amet, consectetur adipiscing elit.</p>");
 $("h2").addClass("white");
 });

$(document).ready(function () {
    // load index page when the page loads
    $("#response").html("DEFAULT");

      $("li").click(function(){
      $(this).addClass("active").siblings().removeClass('active');

      })
    $("#page1").click(function () {
        // load home page on click
        $("#response").html("<h2> Welcome </h2><p>Sed porttitor lectus nibh. Cras ultricies ligula sed magna dictum porta. Praesent sapien massa, convallis a pellentesque nec, egestas non nisi. <br/> Curabitur arcu erat, accumsan id imperdiet et, porttitor at sem. Nulla quis lorem ut libero malesuada feugiat. Lorem ipsum dolor sit amet, consectetur adipiscing elit.</p>");
       $("h2").addClass("white");
    });
    $("#page2").click(function () {
        // load about page on click
        $("#response").html("TESTING 2");
    });
    $("#page3").click(function () {
        // load contact form onclick
        $("#response").html("TESTING 3");
    });
    $("#page4").click(function () {
        // load contact form onclick
        $("#response").html("TESTING 4");
    });
    $("#page5").click(function () {
        // load contact form onclick
        $("#response").html("TESTING 5");
    });

});
li {
display: inline-block;
margin-right: 2px;
}
li a {
padding: 4px;
color:#0086ca;
text-decoration: none;
}
.contentarea {
margin-top: 30px;
width: 100%;
padding: 40px 50px;
background-color: #0086ca;
color: white;
text-align: left;
font-size: 24px;
}
.cd-multi-steps.count li {
counter-increment: steps;
}
.cd-multi-steps.count li > *::before {
content: counter(steps) " - ";
}
@media only screen and (min-width: 768px) {
.cd-multi-steps.text-top.count li > *::before,
.cd-multi-steps.text-bottom.count li > *::before {
/* this is the spot indicator */
content: counter(steps);
height: 26px;
width: 26px;
line-height: 26px;
font-size: 1.4rem;
color: #ffffff;
}
.cd-multi-steps.text-top.count li:not(.current) em::before,
.cd-multi-steps.text-bottom.count li:not(.current) em::before {
/* steps not visited yet - counter color */
color: #2c3f4c;
}
.cd-multi-steps.text-top.count li::after {
bottom: 11px;
}
.cd-multi-steps.text-top.count li > * {
padding-bottom: 34px;
}
.cd-multi-steps.text-bottom.count li::after {
top: 11px;
}
.cd-multi-steps.text-bottom.count li > * {
padding-top: 34px;
}
}
.white{color:yellow}
li.active a{color:blue; border:1px solid;}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<nav>
<ul class="cd-multi-steps text-bottom count">
<li class="current"><a href="#tabs-1" id="page1" class="tab">page 1</a>

</li>
<li class="current"><a href="#tabs-2" id="page2" class="tab">page 2</a>

</li>
<li><a href="#tabs-3" id="page3" class="tab">page 3</a>

</li>
<li><a href="#tabs-4" id="page4" class="tab">page 4</a>

</li>
<li><a href="#tabs-5" id="page5" class="tab">page 5</a>

</li>
</ul>
</nav>
<div class="contentarea" id="response"></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

Is it possible to retrieve the values of #select1 and #select2 before initiating the AJAX request?

I am presented with the following snippet of HTML code: <select id="choice1"> <option value="0">Option 0</option> <option value="1">Option 1</option> <option value="2">Option 2</option> <option value="3 ...

What steps can be taken to resolve the issue with the background's placement?

There seems to be an issue with the border being hidden. I've tried adjusting the background-position variables, but without success. Here is a code example: * { margin: 0; padding: 0; } .container { max-width: 1170px; width: 100%; ...

Button placed within a jade table cell

I'm struggling to make a button appear in each row of the table. I am new to working with Jade and Node.js, so I can't seem to figure out why it's not showing up. Here is my Jade file: html head body table.table.table(border='1 ...

Using <fieldset> allows you to display <select> elements in a vertical arrangement

As I work with this piece of html, JQuery populates my boxes and I am utilizing fieldsets to focus on only the necessary selection before POSTING it to my servlet. However, there seems to be an issue as the content is being displayed vertically when I ac ...

Pictures squeezed between the paragraphs - Text takes center stage while images stand side by side

I'm struggling to figure out how to bring the text between the two images to the front without separating them. The images should be positioned next to each other with a negative square in-between, and the text within this square should be centered b ...

Inconsistent Animation Issue with jQuery Toggle for Expanding Div and Text

My expanding div functionality is almost perfect, but I've noticed that the transition when opening abruptly on divs with text. However, the closing transition is smooth. Can anyone help me make the opening transition as smooth as the closing one? Bel ...

JavaScript Drag Events in Microsoft Edge (Including IE)

My drag event is functioning properly in Chrome, Safari, Firefox, and Opera. However, I encountered an error when running it on Microsoft Edge and IE: SCRIPT438: Object doesn't support property or method 'setDragImage' Below is the code sn ...

How can I fetch and reference multiple local JSON files in Vue using Axios?

I am currently utilizing vue for prototyping some HTML components. Is there a method to make Vue detect two separate JSON files? vue.js var vm = new Vue({ el: '#douglas-laing', data: { products: [], contentPanels: [] }, created() ...

The drop-down menu selection is non-functional on mobile and iPad devices when using Bootstrap

<div class="panel panel-default"> <select> <option value="B3">B3</option> <option value="B4">B4</option> <option value="B5">B5</option> & ...

Utilizing the Google Maps API to geocode addresses and postponing the retrieval of the data

Utilizing Google's maps API to geocode two addresses has brought about a unique challenge for me. I am deferring the returned results and utilizing a $.when().then() method to execute my logic once I receive the coordinates for the string addresses. T ...

Tips on automating the process of moving overflowing elements into a dropdown menu

Challenge Description In need of a dynamic navigation bar, I faced the problem of displaying only the first X items on one line and have the remaining items hidden in a "Show more" dropdown. The challenge was to calculate the width of each item accurately ...

Guide on how to include jquery-ui css file in Vue3

I am new to TypeScript and VueJs. Recently, I decided to incorporate jquery-ui into my project using Vue3, TypeScript, and Electron. After some trial and error, I was able to successfully set up the environment. However, when I attempted to use the jquery ...

Position child divs at the center of the parent div

I am struggling to center 3 child divs inside a parent div that must remain at a width of 100%. Can someone help me figure out how to achieve this? .parent { width: 100%; border: 1px solid blue; } .child { float: left; borde ...

Retrieve a PHP file utilizing Javascript or JQuery

Is there a more straightforward method than using ajax to retrieve the contents of an HTML or PHP file and place it within a div on the current page? I am familiar with the process through ajax, but I am curious if there is a simpler alternative that doe ...

Executing functions during the page loading process

I'm currently in the process of integrating a new payment method into a checkout page that does not have built-in support for it. The button on the page that redirects to an external payment portal requires specific parameters to be passed. I've ...

Tips for embedding a file within a text box in HTML and enabling users to make direct edits

I am currently working on a feature that allows users to open a .txt or .html file from their file explorer and paste the contents into a textarea for editing and saving purposes. My question is whether it's possible to read the file content and auto ...

Issue with onClientClick not functioning properly when performing a jQuery function call

How can I make a jQuery form appear when an ASP.NET server-side button is clicked by the user? Currently, when I click on the button during runtime, the page reloads quickly without displaying the jQuery form. I am aiming to achieve a similar effect show ...

Leveraging JsonResult in combination with jQuery

I am in need of constructing a Json output for a jQuery plugin. The format required is as follows: [{ "name": "Testing", "desc": "OB-2014-0202", "values": [{ "Status": 2, "from": "\/Date(1391295600000)\/", "to": "\/Date(1392505200000)\ ...

Transferring information to an Ionic 5 custom element label

In my ionic 5 app, I have a side menu and tabs. Each page can have different contents for the side menu and the tabs. Instead of duplicating the ion-menu in each page, I created a header component (and one for the tabs) as follows: HEADER COMPONENT: <i ...

The JSON array object gets replaced in the local storage

I have been working on a form page where there are 3 input fields: "fname," "emailId," "phoneNo." Every time a user enters details in the form, I am storing them in localStorage. However, the object keeps getting overwritten. Here is the script: <scri ...