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

Is there a way to position two <div> elements side by side so that they are the same width and height without causing any scrolling?

I am in the process of creating a basic HTML page with two sections, each containing content. However, the last content within the second .right div is extending to the bottom of the page, causing it to become scrollable. I attempted to create another div ...

Rule for jQuery validation based on variables

I am facing an issue with validation of login asynchronously in my HTML form using the jQuery Validation Plugin. I would like to trigger a request for login validity on blur, and validate it upon receiving a response. Below is the code snippet: var login ...

Complications arising from IE when using strict doctype with CSS and jQuery

Here's the code I'm using for a sliding caption effect on my gallery site: I specifically implemented the last effect which is the Caption Sliding (Partially Hidden to Visible). $('.boxgrid.caption').hover(function(){ $(".cover", th ...

Looking for a way to make CSS text color for a:hover have higher priority than a:visited?

I'm having an issue with my CSS code that changes the background color when hovering over a link. The text color is white on a blue background during hover, but if there is no hover, it's blue with a white background. Additionally, once the link ...

Tips for styling a button upon being clicked

Is there a CSS technique to make a button change color when clicked with the mouse? I am aware of using ':hover' for changing color, but I need something specific to a left mouse click. I want the same functionality as a standard button but with ...

Transmitting Data to PHP Using JQuery/AJAX

I'm struggling with this issue. Here is the HTML code that I have: <input type="text" class="studno"><input type="button" value="Check" class="chstudno"> How can I send this data to PHP using JQuery/AJAX? Below is the code that I current ...

What could be the reason that the painting application is not functioning properly on mobile devices?

I am facing an issue with my painting app where it works perfectly on desktop browsers but fails to function on mobile devices. I tried adding event listeners for mobile events, which are understood by mobile devices, but unfortunately, that did not solve ...

Saving Data in an Angular Material Table: A How-to Guide

I have a basic angular material table and I am looking for a way to save the data displayed in each row when a button is clicked. Is it possible to save each row of data as an object and push it to an array? If so, how can I achieve this? <div class=& ...

JQuery UI button causing an error in Internet Explorer related to "potentially harmful Request.Form value"

While testing my application with Internet Explorer, I discovered that buttons styled with JQuery UI button are sending their entire content to the server. This causes a "A potentially dangerous Request.Form value was detected from the client" error in ASP ...

How to style the second child div when hovering over the first child div using makeStyles in Material UI

I am working on a web project with a parent div and two child divs. I need to apply CSS styles to the second child div when hovering over the first child div. Below is the structure of the render method. <div className={classes.parent}> <div c ...

Exploring the distinction between absolute elements nested within other absolute elements and relative elements nested within absolute elements

My latest CSS experiment involved creating a flag of a country. Check out my code: div.flag { width: 900px; height: 600px; background-color: red; position: relative; } div.flag > div { position: absolut ...

Is there a way for me to set distinct values for the input box using my color picker?

I have two different input boxes with unique ids and two different color picker palettes. My goal is to allow the user to select a color from each palette and have that color display in the corresponding input box. Currently, this functionality is partiall ...

Is it possible for microdata to function properly on a non-HTML5 website?

I am looking to incorporate microdata into my website, which currently does not use HTML5 but instead uses <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> Although I have plans to switch ...

The art of perfect alignment: Mastering the positioning of Material-UI Grid

This issue has been resolved Hey there, I'm currently working on a React App with Material-UI and I'm trying to center a Grid item that includes a Card along with two additional Grid items. Below is the relevant code snippet. Although the Cards ...

Finding the text within a textarea using jQuery

My journey with jQuery has just begun, and following a few tutorials has made me feel somewhat proficient in using it. I had this cool idea to create a 'console' on my webpage where users can press the ` key (similar to FPS games) to send Ajax re ...

What methods can I use to prevent jquery.address from modifying the URL in specific scenarios?

I am utilizing a plugin called jquery.address to manage browser history states on my ajax-heavy website. However, there is a specific scenario where I need to prevent the $.address.change() function from being triggered by a certain link. For instance, in ...

Achieving the grid-column property as you navigate deeper into the tree

Currently, I am in the process of integrating a CSS design that utilizes grid and grid-column into my Angular application. The design itself is quite effective, structured similar to this: .grid { display: grid; grid-template-columns: repeat(3, 1 ...

What is the best way to utilize jQuery for submitting a form through ajax and then extracting and interpreting the response?

My code has been simplified to include only the necessary elements for this example. I am able to submit the form successfully, but I am looking to understand how to handle error responses so that I can parse and display them. This is what I have attempte ...

The Link Breaks the Overlay Hover Effect

Currently, the code functions as intended when you hover over or touch the thumbnail, an overlay will appear. The issue lies in the fact that to navigate to a specific URL, you have to click directly on the text. The overlay itself is not clickable and ca ...

Step-by-step guide on duplicating a div a set number of times

I am looking to replicate the entire div multiple times (e.g., on an A4 paper, top left corner, top right corner, bottom left corner, and bottom right corner). <script language="javascript" type='text/javascript'> function updateD ...