Tips on how to update the styling of an active link

http://jsfiddle.net/G8djC/2/

Looking to create a tabbed area where content changes based on the tab clicked. The Javascript function switches the link class to active upon clicking. However, struggling to change the color of the active tab beyond the first click.

Tried various solutions but unable to pinpoint the issue causing it not to work as intended.

CSS:

/* hide inactive tabs */
#content_2, #content_3, #content_4, #content_5 {
 display: none;
}

/* list styling */
ul.tabs li {
 list-style: none;
 display: inline-block;
}

/* inactive links */
ul.tabs li a {
 background-color: black;
 display: inline-block;
}

/* active links */
ul.tabs li a.active {
 background-color: pink;
 display: inline-block;
}

HTML:

<div class="all-interact">
<h1>Title</h1>

<div class="inner-interact">

    <ul class="tabs">
       <li><a href="javascript:tabSwitch('tab_1', 'content_1');" id="tab_1" class="active">tab 1</a></li>
       <li><a href="javascript:tabSwitch('tab_2', 'content_2');" id="tab_2">tab 2</a></li>
       <li><a href="javascript:tabSwitch('tab_3', 'content_3');" id="tab_2">tab 3</a></li>
       <li><a href="javascript:tabSwitch('tab_4', 'content_4');" id="tab_2">tab 4</a></li>
       <li><a href="javascript:tabSwitch('tab_5', 'content_5');" id="tab_2">tab 5</a></li>
    </ul>


        <div class="tab-content" id="content_1">
            <p>tab 1 content</p>
        </div>

        <div class="tab-content" id="content_2">
            <p>tab 2 content</p>
        </div>

        <div class="tab-content" id="content_3">
            <p>tab 3 content</p>
        </div>

        <div class="tab-content" id="content_4">
            <p>tab 4 content</p>
        </div><!--content4-->

        <div class="tab-content" id="content_5">
            <p>tab 5 content</p>
        </div><!--content5-->

    </div>
</div>

Javascript:

function tabSwitch(new_tab, new_content) {

document.getElementById('content_1').style.display = 'none';
document.getElementById('content_2').style.display = 'none';
document.getElementById('content_3').style.display = 'none';
document.getElementById('content_4').style.display = 'none';
document.getElementById('content_5').style.display = 'none';
document.getElementById(new_content).style.display = 'block';

document.getElementById('tab_1').className = '';
document.getElementById('tab_2').className = '';
document.getElementById('tab_3').className = '';
document.getElementById('tab_4').className = '';
document.getElementById('tab_5').className = '';
document.getElementById(new_tab).className = 'active';

}

Answer №1

Only the first id is different from the rest, which are all set to tab_2:

<ul class="tabs">
   <li><a href="javascript:tabSwitch('tab_1', 'content_1');" id="tab_1" class="active">tab 1</a></li>
   <li><a href="javascript:tabSwitch('tab_2', 'content_2');" id="tab_2">tab 2</a></li>
   <li><a href="javascript:tabSwitch('tab_3', 'content_3');" id="tab_2">tab 3</a></li>
   <li><a href="javascript:tabSwitch('tab_4', 'content_4');" id="tab_2">tab 4</a></li>
   <li><a href="javascript:tabSwitch('tab_5', 'content_5');" id="tab_2">tab 5</a></li>
</ul>

Updated version:

<ul class="tabs">
   <li><a href="javascript:tabSwitch('tab_1', 'content_1');" id="tab_1" class="active">tab 1</a></li>
   <li><a href="javascript:tabSwitch('tab_2', 'content_2');" id="tab_2">tab 2</a></li>
   <li><a href="javascript:tabSwitch('tab_3', 'content_3');" id="tab_3">tab 3</a></li>
   <li><a href="javascript:tabSwitch('tab_4', 'content_4');" id="tab_4">tab 4</a></li>
   <li><a href="javascript:tabSwitch('tab_5', 'content_5');" id="tab_5">tab 5</a></li>
</ul>

JSFiddle

Answer №2

all tabs now have unique IDs. Check out the updated code:

 <ul class="tabs">
   <li><a href="javascript:tabSwitch('tab_1', 'content_1');" id="tab_1" class="active">tab 1</a></li>
   <li><a href="javascript:tabSwitch('tab_2', 'content_2');" id="tab_2">tab 2</a></li>
   <li><a href="javascript:tabSwitch('tab_3', 'content_3');" id="tab_3">tab 3</a></li>
   <li><a href="javascript:tabSwitch('tab_4', 'content_4');" id="tab_4">tab 4</a></li>
   <li><a href="javascript:tabSwitch('tab_5', 'content_5');" id="tab_5">tab 5</a></li>
</ul>

view fix here

Answer №3

Having identical ids #tab_2 for 4 out of 5 anchor tags seems like a common mistake we all make at some point. :)

A helpful tip is to validate your markup using an HTML validator with a template that includes all the necessary information filled in, which can quickly identify such errors.

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
   "http://www.w3.org/TR/html4/strict.dtd">
<HTML>
   <HEAD>
      <TITLE>My first HTML document</TITLE>
   </HEAD>
   <BODY>
      <P>Hello world!
   </BODY>
</HTML>

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

Converting Milliseconds to a Date using JavaScript

I have been facing a challenge with converting milliseconds data into date format. Despite trying various methods, I found that the solution provided in this link only works for a limited range of milliseconds values and fails for higher values. My goal is ...

A comprehensive guide on adjusting the height of images dynamically using only CSS

Calling all CSS experts: I'm curious if it's doable to replicate the image scaling effect seen on this website using only CSS? Please note that the images only begin scaling when the height of the browser window changes, not its width. (I menti ...

Looking to display the ng-option in two lines within Angular 6?

How can I display data in a select dropdown with two lines for each option? I am currently using ng-select. <ng-select [(ngModel)]="selectedData" placeholder="Select Data"> <div *ngFor="let data of Data"> <ng-option [value]="da ...

Utilizing Shopify API to seamlessly add items to cart without any redirection for a smoother shopping experience

Looking for a solution to submit an add to cart POST request without any redirection at all? I have tried changing return_to to "back" but that still reloads the page, which is not ideal. My goal is to smoothly add the item to the cart and notify the cli ...

Element in the Middle

I have just started learning HTML and CSS, and I'm curious to know how I can center the links Home, About, and Contact in the header. HTML: <body> <header> <h1><a href="#">HBT</a> </h1& ...

Internet Explorer 11 fails to interpret the attribute attr.xlink:href

I am a beginner in Angular 2 and I'm working on creating an icon component. The code I have below works perfectly in Chrome and Firefox, but unfortunately, it's not functioning in Internet Explorer 11. Here is what my component looks like: @Com ...

Black-colored backdrop for Mui modal

Currently working with a mui modal and encountering an issue where the backdrop appears as solid black, despite setting it to be transparent. I attempted to adjust the color to transparent, but the issue persists. ...

Data is not being stored in the MongoDB Model/Schema as expected

I'm encountering an issue with my MongoDB database. It appears to not be receiving the data I am attempting to send to it, resulting in an empty database despite everything else functioning smoothly. The application I'm working on involves scrap ...

Unable to retrieve JSON data from converting TXT using JavaScript, resulting in undefined output

After converting txt to JSON, I encountered an issue. const txt = JSON.stringify(`{ ErrorList: [{ 80: 'Prepared' }], Reference: [ { 'Rule Name': 'Missing 3', 'Rule ID': 1, 'Rule Des& ...

Adding child arrays to a parent array in Angular 8 using push method

Upon filtering the data, the response obtained inside the findChildrens function is as follows: My expectation now is that if the object length of this.newRegion is greater than 1, then merge the children of the second object into the parent object's ...

Express-Session Object Method

I am currently facing an issue with linking an object to an Express session. Below is the code I am using: var express = require('express'); var session = require('express-session'); // Defining an object named "engine" which simulate ...

Is it necessary to use callbacks when using mongoose's findbyid with express to retrieve an object from the database? Are callbacks still important in modern JavaScript frameworks?

I'm currently exploring the express local library tutorial on MDN docs and wanted to try out returning an object without relying on a callback method. When I provide the request object parameter for the id to the findById mongoose method like this va ...

What is the best way to invoke a Javascript function within the same file using PHP?

I am facing an issue with my PHP file named "PhpCallJavascript". I am attempting to call the function CreateSVG() from within the PHP code. However, it seems that it is not working. Do I need to incorporate AJAX here? Or perhaps there is another solutio ...

Express.js code is experiencing difficulties with autocomplete functionalities

jQuery autocomplete code example [Express JS code for retrieving data][2\ Example of fetching data in Express JS ...

"Displaying the y-axis in d3.js: A Step-by-Step

I am a beginner in d3.js and I'm having trouble with my y-axis not showing up in the browser. Can someone please help me find a solution? var barOffset=5; var barWidth=50; var width=700,height=700; function processData(data,key){ var objects=[]; ...

JavaScript for creating dropdown menus using Twitter Bootstrap

I've been struggling to get the dropdown menus in my Twitter Bootstrap project to function properly. Below is the code I have for the navbar: <div class="container-fluid"> <div class="row-fluid"> <div class="span12"> < ...

Discover the correct steps to transition from using particles.js to the react-tsparticles package

Migrating from react-particles-js to react-tsparticles-js Hi there! I'm currently facing an issue with my ReactJS website here, which is deployed using Netlify. The error code below keeps popping up, and I believe it's related to the transition ...

What is the best way to transfer data between different states in ReactJS components?

I am in the process of developing an application that will showcase a list of categories. Upon selecting a category, the application will transition to the next page where a query regarding water type will be posed. Once the water type is chosen, the app s ...

I'm facing a challenge with retrieving the controller's scope in my Angular directive

Having trouble accessing the settings I receive from the server in my app. The directives are set up correctly and the $http is used to fetch a js file with the app settings. However, despite being able to log the scope in the console, I am unable to acces ...

The WebRTC video feature is functioning on the local network but is encountering difficulties

Trying to grasp WebRTC has been quite the journey for me. In an attempt to troubleshoot my failed video transfer between computers, I uploaded what I have so far onto a temporary github repository: https://github.com/stevendesu/webrtc-failure My goal is ...