How can we make a link stand out when clicked in jQuery, and additionally display a hidden element?

Check out the codepen: http://codepen.io/tristananthonymyers/full/BRPmKa/

The goal is to have the ".active-link:after" style applied only to the clicked link and show the corresponding page like "#about-page". However, the issue is that the "#about-page" is showing but all links have the ".active-link:after" style applied to them.

While I may not be an expert in Jquery, I feel confident with HTML and CSS.

HTML:

<div class="header">
 <p class="placeholder-logo logo">Tristan Myers</p>
 <p class="placeholder-logo-text logo">Front-end Web Developer/Designer</p>
</div>
<div class="nav-links"> 
 <a class="nav-link" href="#" id="about-link">About Me</a>
 <a class="nav-link" href="#" id="work-link">My Work</a>
 <a class="nav-link" href="#" id="contact-link">Contact Me</a></div>
<div class="page" id="about-page">
 <p>this is about page.</p>
</div>
<div class="page" id="work-page">
 <p>this is work page.</p>
</div>
<div class="page" id="contact-page">
 <p>this is contact page.</p>
</div>

CSS:

.show {
  visibility: visible; 
 }  
.active-link:after {
  background: white;
  width: 100%;
  height: 1px;
}

JQUERY:

var navLink = $('.nav-link')
var aboutLink = $('#about-link');
var workLink = $('#work-link');
var contactLink = $('#contact-link');
var aboutPage = $('#about-page');
var workPage = $('#work-page');
var contactPage = $('#contact-page');

navLink.click(function togglePage(about, work, contact) {
 toggleAboutPage(about);
 toggleWorkPage(work);
 toggleContactPage(contact);
 return false;
});

function toggleAboutPage() {
 aboutPage.toggleClass('show');
 aboutLink.toggleClass('active-link');
};

function toggleWorkPage() {
 workPage.toggleClass('show');
 workLink.toggleClass('active-link');
};

function toggleContactPage() {
 contactPage.toggleClass('show');
 contactLink.toggleClass('active-link');
};

Answer №1

This is a sample code snippet on how to achieve this functionality:

$(".tab-link").on("click", function(){
  var tab_index = $(".tab-link").index($(this));
  
  $(".tab-link").removeClass("active-tab");
  $(this).addClass("active-tab");
  
  $(".content").removeClass("show");
  $($(".content").eq(tab_index)).addClass("show");
});

Check out the demo on CodePen

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

Numerous attributes for displaying ngOption values

I have an item that resembles the following: $scope.team = [ { name: "John", number: 1 }, { name: "Emma", number: 2 } ]; Currently, in my HTML code, I am using ngOption to populate a dropdown menu with values from the object. < ...

Handle improperly formatted XML in Perl using Perl-XML

Using the perl command line utility xpath, I am extracting data from HTML code in the following manner: #!/bin/bash echo $HTML | xpath -q -e "//h2[1]" The HTML is not well-formed, causing xpath to throw the error below: not well-formed (invalid token) a ...

What is the best way to darken the background when an alert is displayed and disable the dimming effect when the alert is closed?

Here's the JavaScript snippet I'm using: reset.addEventListener("click", function(){ document.querySelector("#body").classList.add("darkenPage"); myReset(); alert("Reset Successful!!"); document.querySelector("#body").classList.re ...

What is the best way to apply maximum height to an element using CSS?

Looking for help with my website: I made some changes in the code using Firebug and identified these elements that I want to modify (but changes are not live yet) The following code represents the elements on my site: This is the HTML code: <di ...

HTML link with "mailto:" not opening in a new tab

Just posted for the first time! I'm attempting to create a mailto link using 'templated' JavaScript that pulls specific data from a JSON object: var menu = { "menu": [ { "title": "let's talk", "link": "mailto:<a href ...

Tips for displaying text within an input field labeled "password" and then reverting back to a password display after entering text

I am working with a login form that includes a password field: <input type="password" name="password" id="password" value="Password" maxlength="40" required style="width: 130px; font-size: 10px;" /> Currently, the password field displays "******** ...

What is the reason for jQuery's inability to recognize ":not(#menu *)" in a selector?

I have created a Javascript-based navigation bar that is triggered by clicks instead of hover for better mobile usability. I have made sure to keep the HTML, CSS, and JavaScript code as simple as possible. To ensure that clicking anywhere outside the menu ...

Choose the first element of its type and disregard any elements nested within it

I need to change the color of only the first p element within a specific div. Using :first-child would work in a simple example, but in my project, there are multiple elements and more could be added in the future. I want to avoid using :first-child alto ...

Is it possible to include two of these on a single page with unique variables? (Using JQuery)

I am looking to create two similar pages, each with different percentages. However, when I try modifying the JS or changing class/ID names, it keeps pulling data from the first SPAN element. http://jsfiddle.net/K62Ra/ <div class="container"> <di ...

Modify flex direction to column in response to changes in height of another div or when its content wraps

I am currently utilizing React MUI V5 and I am looking to modify the flex direction of a div from row to column when a preceding div changes in height or wraps. Below is a basic example of what I have implemented using plain HTML/CSS, but I am uncertain i ...

The Nivo Slider experiences compatibility issues with webkit browsers when used in conjunction with Really Simple History (RSH) technology

I'm in the process of developing a website with AJAX capabilities, utilizing the Really Simple History (RSH) framework to manage back and forward requests. One challenge I've encountered is that when using Nivo Slider for a slideshow feature, it ...

Ways to determine if a string or HTML contains repeated consecutive elements

Assume I am working with the following string <div id="ch">abcdefg<img /><img />hij</div> <div id="ad">abc<img />defg<img />hij</div> strHtml = $('div#ch').html(); strHtmlFalse = $('div#ad&ap ...

The layout option is not specified within the ejs-mate package

error boilerplate I am baffled as to why the layout is not being defined. I have installed ejs-mate and ejs, yet it still gives this error. <% layout('layouts/boilerplate') %> <h1>All campgrounds </h1> <div> <a ...

When the header is given a fixed position, the modal becomes unattainable

I am currently working on developing my personal news website called News Pews. This is my third project, and I have encountered an issue that was not present in my previous projects. The problem arises when I apply position: fixed; top:0; to the header c ...

Exploring the Excitement of PHP Regular Expressions (preg_replace)

Looking for assistance with a regex preg_replace function to clean up HTML content being submitted to my app's controller/model. The goal is to strip away unwanted HTML and convert specific elements into proprietary tags for the app. $postText = $_PO ...

Problem saving videos when using the right-click function

Hello, I am currently working on retrieving videos from a database, but I have encountered an issue with the video saving option. When a user right-clicks on the video, they are able to save it in their system's storage. What I would like to achieve ...

Unique Floating Bullet Icons Ascending When Enlarged

I'm currently trying to use a star image as a custom bullet point on an <li> element. However, I'm facing the issue where the bottom of the image aligns with the font's baseline, causing the image to be pushed upwards. Is there any sol ...

What is the best way to make a panel width adjust automatically to fit the screen width?

I have developed a React/Material UI application that includes a Portal which showcases a Panel and a Widget. Main App: Show Portal and Set Background Color to Blue export default function App() { return ( <div style={{ backgroundC ...

only execute the function once

I have implemented the following code to trigger a lightbox show and callback function_a, however, it is currently firing 16 times. Is there a way to ensure that it only fires once? $("#open").click(function(){ $('.a, .b, .c, .d').fadeOut(6 ...

occupying half of the screen

Having trouble displaying two videos in an ionic grid. My goal is to have both videos take up the entire screen, with each occupying 50% height and 100% width. However, when I try to achieve this, the ion-row only takes up 50% of the screen and the video i ...