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

Sharing an array with a child component using the @Input decorator

I am attempting to send an array to a child component. The array is created within the subscribe method of the onInit lifecycle hook in the parent component. Parent component: import { Component, OnInit } from '@angular/core'; @Component({ s ...

Load an XML file from the local server asynchronously on the Chrome web browser

Attempting to load a local XML/XSL file into a variable for editing seems to be causing an issue. The code provided functions properly in IE and Chrome, however, Chrome displays a warning due to the synchronous nature of the call. function loadXMLDoc(fileN ...

The AJAX call failed because the web service was not properly configured, resulting in a missing parameter value being

I'm encountering an issue with my ajax call that displays a specific message. [WebMethod(EnableSession = true)] public static string UpdateTotalPrice(List<TicketPriceAjax> jsonTickets) { // conducting server-side processing return "a u ...

What issues are present with the JavaScript event management in this scenario? (Specifically using the click() and hover() jQuery functions)

Currently, I am in the process of developing a proof-of-concept for a project that mimics Firebug's inspector tool. For more detailed information, please refer to this linked question. You can view an example page of my work which has only been teste ...

Is there a way to retrieve the id of every post on my page?

Is it possible to send multiple post ids in JavaScript? I have successfully sent the first post id, but now I need to figure out how to send each individual post id. When inspecting the elements, I see something like this: <div data-id="post_1">< ...

Is there a way to insert an attribute in HTML without simply appending it to the end of the tag?

I've been working on incorporating the code snippet below that I found in an angularjs table sort function. In the code, you can see the ts attributes being added to the html. However, this method is not suitable for hosting on Salesforce. Can anyone ...

The Bootstrap image fails to adhere to the size of its parent flex container

I am having trouble positioning an image, heading, and a button on top of another image. The issue arises when viewing the content on small screens as the smaller image fails to resize properly and extends beyond the background of the larger holding imag ...

I am experiencing an issue with mydaterangepicker and primeng where it is not displaying properly in the table header. Can anyone assist me with this

I am attempting to integrate mydaterangepicker () with primeng turbotable (since primeng calendar does not meet the requirements), but I am having trouble with its display. Could you please assist me with some CSS code or suggest an alternative solution? ...

Display data when clicking on Tailwind

I am currently displaying a sub menu on hover using Tailwind CSS. However, I am wondering how I can achieve the exact same functionality by triggering an onclick event instead of hovering over the menu. Here is a DEMO showcasing the current setup. CODE: ...

How can a function work with an array of subarrays of objects without altering the original array?

Within a small coding project I recently completed, I handled an array consisting of 3 subarrays, with each subarray containing 4 objects. I passed this array through a function that gradually removes values from each subarray until only 1 object remains i ...

What is the process for triggering property decorators during runtime?

Wondering about dynamically invoking a property decorator at runtime. If we take a look at the code snippet below: function PropertyDecorator( target: Object, // The prototype of the class propertyKey: string | symbol // The name of th ...

The IntroJs step is only partially visible on the screen

Currently, I am incorporating introJS into my application and encountering an issue where one of the steps is only partially visible on the screen. Despite trying various position settings such as auto, left, right, etc., this particular item consistentl ...

Dynamic background image changes when checkbox is selected

Greetings! I am a newcomer to the stackoverflow community and I am facing an issue with dynamically changing the background image of my webpage when a checkbox is checked. Here is the HTML code snippet: <ul> <li><label for="Lines">A ...

Next.js Head component will not repeat the same Meta Tags

In my Next.js project, I have implemented different meta tags with various media targets in the Head section: <Head> <meta name="theme-color" media="(prefers-color-scheme: light)" content="#7f8fa6"/> <meta name= ...

What is the best way to adjust the width of a navbar using JavaScript?

I am experimenting with a responsive design and facing an issue - the width of my #menu in CSS is initially set to 0, but I need to change this value based on user input using JavaScript. Now, I want to dynamically adjust the width of the menu for differe ...

Tips on customizing the appearance of two Material-UI Sliders across separate components

Looking to customize two sliders, each within their own react component. Slider in the first component const customizedThemeSlider1 = createTheme({ overrides:{ MuiSlider: { thumb:{ color: "#4442a9", marg ...

What is the best way to isolate the CSS of individual components in Angular 2?

For the first component: CSS-- ngx-dnd-container { color:black; background-color: white; } HTML- <ngx-dnd-container [model]="targetItemsA" dropZone="multiple-target-a" </ngx-dnd-container> For the sec ...

How can I design a versatile button in HTML/CSS3/jQuery/Javascript that utilizes images for borders and corners for maximum scalability?

Is there a way to create a completely scalable button in HTML/CSS3/jQuery+Plugins using images for borders and corners? I have attempted a messy method, but I am confident that there are more effective solutions available. The approach I considered invol ...

Tips for sending a parameter to a URL from a controller using AngularJS

I am currently working on a feature where I need to combine adding and editing functionalities on one page. The items are listed in a table below, and when the edit button is clicked, I want to pass the ID of that specific item in the URL. This way, if the ...

What is the best way to bring a module into an Angular project?

I have a project in Angular with an additional module created as an npm package. The structure of the module is as follows: --otherModule --other-module.module.ts --index.ts --package.json index.ts: export { OtherModule } from './other-module ...