Active Tab Indicator - Make Your Current Tab Stand Out

I currently have a set of 3 tabs, with the first tab initially highlighted. I attempted to use some JQuery code in order to highlight a selected or active tab, but for some reason it is not working as expected.

Any assistance would be greatly appreciated.

Thank you

<div class="tabs tab-active" id="tab-part1">Etiam</div>
<div class="tabs" id="tab-part2">CatM1(LTE)</div>
<div class="tabs" id="tab-part3">LTE</div>
<div class="tab-parts">
<div id="part1">
This is part1
</div>
<div id="part2">
This is part2
</div>
<div id="part3">
This is part3
</div>
</div>

JS

 $(function () {
     $('#tab-part1').click((event) => {
            $(this).siblings().removeClass('tab-active');
        $(this).addClass('tab-active');
        $(this).find('.tab-parts #part2, .tab-parts #part3').hide();
        $(this).find('.tab-parts #part1').show();
     });
     $('#tab-part2').click((event) => {
        $(this).siblings().removeClass('tab-active');
        $(this).addClass('tab-active');
        $(this).find('.tab-parts #part1, .tab-parts #part3').hide();
        $(this).find('.tab-parts #part2').show();

     });
     $('#tab-part3').click((event) => {
        $(this).siblings().removeClass('tab-active');
        $(this).addClass('tab-active');
        $(this).find('.tab-parts #part1, .tab-parts #part2').hide();
        $(this).find('.tab-parts #part3').show();
     });
 });

Here is the jsfiddle link

Answer №1

If you want to handle different tabs in one event handler without creating multiple click handlers, you can do it like this:

$(function () {
     $('.tabs').click((event) => {
        const tab = $(event.target);
        const activeTab = $('.tab-active');
        activeTab.removeClass('tab-active')
        tab.addClass('tab-active');
     });
 });

Note that additional logic will be required to show/hide the text below the tabs. The focus here is on solving the issue of handling active tabs.

You can view a working example at: http://jsfiddle.net/so17whdc/

I hope this solution helps you with your task.

Answer №2

If you want to replace your (event) with a function, keep in mind that using (event) => $(this) will make 'this' reference the event itself, not the element you are targeting.


$(function () {
     $('#tab-part1').click(function(){
            $(this).siblings().removeClass('tab-active');
        $(document).find('.tab-parts #part2, .tab-parts #part3').hide();
        $(this).addClass('tab-active');
        $(document).find('.tab-parts #part1').show();
     });
     $('#tab-part2').click(function() {
        $(this).siblings().removeClass('tab-active');
        $(this).addClass('tab-active');
        $(document).find('.tab-parts #part1, .tab-parts #part3').hide();
        $(document).find('.tab-parts #part2').show();

     });
     $('#tab-part3').click(function(){
     $(this).siblings().removeClass('tab-active');
            $(this).addClass('tab-active');
        $(document).find('.tab-parts #part1, .tab-parts #part2').hide();
        $(document).find('.tab-parts #part3').show();
     });
 });

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 dropdown menu displaces nearby content

Visit this website for more information To access the blog section, simply click on "BLOG" in the top menu. Once there, look to the right side under the circle logo and select "Dísznövények" from the dropdown menu. Be cautious of long content strings b ...

Root location for offline and pre-production in the Backbone boilerplate router

I am currently utilizing the backbone boilerplate found at https://github.com/tbranyen/backbone-boilerplate My development process involves working on static HTML/JS files offline and conducting tests offline before deploying them to another site for pre- ...

What is the best way to incorporate HTML styling into a PHP email tutorial focused on Swift Mail?

I recently created a competition page for a client, and they have requested that the email sent to customers be more visually appealing than just plain text. The tutorial I followed only included basic text within the 'send body message' section. ...

Utilize Angular to transform a JSON string into HTML text featuring organized bullet points

I am currently working on a project using Angular and I need to display some data from a JSON file on a webpage. The issue I am facing is that the message is quite lengthy, and I would like it to be presented in bulleted points. "messageToDisplay" ...

Button is nested within a closing div causing it to trigger independently without affecting the parent

I have implemented a slideup and slidedown system with some fading effects, but I am encountering an issue with the slide up function. It doesn't seem to reset the display property back to none. For reference, here is a Fiddle link: Slide Up/Down Fid ...

Preventing blank text entries in a task management application

Is there a way to determine if the text input provided by the user is empty or contains some text? I'm in the process of developing a TO-DO app and I'd like it so that if a user fails to enter anything into the text area and clicks on "add", the ...

Creating a lifelike sinusoidal flag animation or flutter effect using SVG格式

I'm attempting to create a lifelike animated svg flag that flutters in the wind. Currently, I am using this simple flag base: <svg xmlns="http://www.w3.org/2000/svg" width="200px" height="100px" viewBox="0 0 200 100" > <p ...

Arranging the rows and columns of a table in optimal alignment

I am currently facing an issue with two tables. The first table consists of 2 rows and 4 columns, with the first column spanning 2 rows. However, in the visual representation, the last 3 columns are misaligned with the rows. How can this alignment be corre ...

Automatically inserting a row into an HTML table

My table structure looks like this: <form name="frm_data_nasabah2" method="post" enctype="application/x-www-form-urlencoded" action="<?php echo $page_action;?>"> <table class="table table-bordered table-hover" id="tab_logic_ ...

Unauthorized access detected during ajax request triggered by onpaste event

Recently, I encountered an issue with my asp.net mvc website where an ajax call to the server stopped working in IE 11, despite previously working fine in IE 8. The error message pointed to an access denied exception in jQuery 1.7.1 at h.open(c.type,c.url, ...

What is the correlation between statistics and posts that are generated dynamically?

One interesting aspect to consider is how statistics are connected to individual posts. For instance, linking the like count to each Facebook post or tying upvote and downvote stats to a question on Stackoverflow presents some intriguing challenges. How ...

Launch the game within the gaming application

I am looking to incorporate a game within another game... Here's what I mean: Open the app: Pandachii Next, navigate to the 4th button (game pad). When we click on the game icon, I want to launch another game (located below the Pandachii window - c ...

CSS gallery slideshow with images that smoothly fade-in and fade-out at a specified location

In the image below, at position 4 (marked by yellow circle) from the left where picture 6 is displayed, I want a cross-fade (fade-in/fade-out) gallery of images to take place. Specifically at position 4, I am looking for a cross-fade (fade-in/fade-out) ef ...

Does Firebase only send a single input value instead of a Span?

I am currently facing an issue with pushing data into my firebase database. I have successfully incorporated one user input field into the process. However, now I am attempting to include a span value in my database, which changes based on a slider that co ...

I am looking to implement screen reader capabilities for both the select-2 combo box and bootstrap's datetime-picker. How can I go about doing

On my existing website, I have select-2 combo boxes for drop-down lists and a Bootstrap Calendar widget. While these features work well, they lack accessibility for users with disabilities. In an effort to make the website inclusive for everyone, I am work ...

Ways to modify the CSS of an active class within a child component when clicking on another shared component in angular

In my HTML template, I am encountering an issue with two common components. When I click on the app-header link, its active class is applied. However, when I proceed to click on the side navbar's link, its active class also gets applied. I want to en ...

primary and secondary colors within the Material UI design scheme

Is there a way to apply different colors for Cards and Papers in the Material UI Theme that are compatible with both light and dark modes? In my app, I have a list of Cards placed on top of a Paper background. Currently, the default color for both the Pap ...

Attempting to retrieve currentScript is causing a typeError to be thrown

I'm attempting to access a custom attribute that I added to my script tag: <script type="text/javascript" src="https://.../mysource.js" customdata="some_value"></script> To make this work on Internet Explorer ...

Learn how to send multiple checkbox values using jQuery and AJAX requests

When trying to extract the value from multiple checkboxes, I utilize this particular code snippet: <form class="myform" method="post" action=""> <input type="checkbox" class="checkbox" value="11" /><br> <input type="ch ...

Can the navigation bar be filled automatically with content that corresponds to the anchors found within the content?

Currently, I am utilizing a Bootstrap frontend for the project at hand. The task I am tackling involves a significant amount of automatically generated content and code, which greatly simplifies the work process. My goal is to explore the potential of cre ...