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

Struggling with running a jQuery ajax request inside a function?

Below is my code for a jQuery Change Event: $("input[name=evnt_typ]").change(function(){ var request = $.ajax({ method: "POST", url: "ajaxRequest.php", dataType: "json ...

Animation triggered by scrolling is not functioning/displaying div

I'm attempting to make a div fade up when it enters the viewport by using the library found at https://github.com/michalsnik/aos Unfortunately, all that seems to happen is that the div gets hidden. In the head section of my HTML file, I've refe ...

What is the best way to implement a CSS rule that is influenced by the number of siblings present within an element?

Is there a way to style an element differently based on the number of subelements it contains? <div class="wrapper"> <div class="element" /> </div> or... <div class="wrapper"> <div class="element" /> <div class="el ...

What could be causing the background color not to change on the HTML page with Bootstrap?

Learning html, bootstrap, and css styling can be a confusing endeavor. Sometimes things don't work as expected, like changing the background color. Despite following what seems like a simple solution after googling, the background color remains unchan ...

The combination of Jquery CSS selectors, plugins, and jqGrid allows for enhanced

I have successfully used multiple plugins that target CSS selectors, but I am facing an issue when trying to apply the same technique to the jqgrid plugin in edit mode. Let me provide some context: In a separate JS file, I have added the following code: ...

Issue with rendering components list in React.js

I am currently working on a project using React, and I'm facing an issue where my list is not displaying on render(). In my parent component, I have a list of components coming from SearchResult. Below is the relevant portion of my code: class Create ...

Using jQuery to Create and Export XLS File

Need assistance with exporting data to an xls file sorted by date. Unsure how to resolve this issue. Here is the jQuery Code : $('#report_xls').live("click", function(){ var url = "ticket.report.php"; var startdate = $('input:text[ ...

I am interested in implementing a textbox search feature using AJAX

Hey there, I'm currently trying to implement a live search feature using AJAX in my textbox and then update the page with data retrieved from the database. However, I would like the output to be displayed with the following headers: <t ...

How can I retrieve the value from an input form using jQuery if it returns null?

Despite trying various methods, I have been unsuccessful in retrieving form values using jQuery and sending them in an AJAX GET request. The value keeps showing as null even after spending more than 18 hours on it. Any help would be greatly appreciated. $ ...

New messages are revealed as the chat box scrolls down

Whenever a user opens the chatbox or types a message, I want the scroll bar to automatically move down to show the most recent messages. I came across a solution that seems like it will do the trick: The issue is that despite implementing the provided cod ...

Discover the process of extracting HTML content that's stored within a JavaScript variable

Having a HTML snippet stored in a variable, like this: var parse= 'Hello<i class="emoji emoji_smile" title=":smile:"></i><i class="emoji emoji_angry" title=":angry:"></i>World' I am looking to extract the value of the "t ...

Using JavaScript to submit the value of a checkbox

I'm currently working on a form submission using JavaScript that includes both text input and two checkboxes. <script> function SubmitFormData() { var preferredLocation = $("#preferred_location").val(); var relocation = []; ...

Tips for choosing an element using a series of ids in jQuery

I am facing an issue with my HTML code. Here is the snippet: <div id="outer"> <div id="inner">...</div> </div> Previously in Prototype, I could easily select the inner div by its ID like this: $('#outer #inner'); H ...

Accessing a webpage by directly pasting the URL into the address bar is functioning properly, but is

I'm facing an issue with accessing files from a vendor's application. When I directly enter the endpoints into the browser's address bar, the file opens up without any problems. However, when I try to access them using jQuery AJAX, I receive ...

The "hover" effect is not activating on a carousel

I'm having trouble with the hover effect inside the orbit slider. It's not working at all. What am I missing here? Check out the code and fiddle: http://jsfiddle.net/Bonomi/KgndE/ This is the HTML: <div class="row"> <div class="la ...

Ways to adjust the text size in jqGrid?

The current theme is ui-lightness. How can I adjust the font size within the grid? Your suggestions are appreciated. Many thanks. ...

Create a dynamic feature that allows users to easily add text fields within a form in

I'm looking to create a form that adds another input text field automatically when the user fills out one. Here is an example of the HTML structure: <form method="post" action="http://localhost/save" class="save"> <fieldset> <div&g ...

Filling out the form will automatically direct you to the text input section

I'm trying to figure out if I can create an input box in HTML that directs the user to a specific HTML file based on the word they enter. For example, if they type "Doctor", it would redirect them to the page doctor.html. This is for a school project ...

Indent the text within a span element that is displayed as an inline block

I am in search of a solution using only CSS for the following issue. Take into account the HTML below: <p> <span>Some text</span> <span>Some text</span> </p> Both <span> elements are set as inline-block. ...

How can I arrange an ItemTemplate and Alternating ItemTemplate next to each other in a Gridview layout?

My webpage features a "Reports" page that will dynamically display buttons (formatted ASP:Hyperlinks) based on the number of active reports in a table. While everything works well, I am struggling with achieving the desired layout. I want my buttons to app ...