I created a customized version of jQuery tabs, but I am looking for an external link to display the tab content and to style the original navigation

I've been experimenting with modifying a tabs script in jQuery that I came across. It seems like my attempt to enhance it has made things more complex than necessary. While I know creating tabs with jQuery is simple, I wanted to create my own version for learning purposes. The vertical tabs are functioning properly, but I also want the bottom links to behave the same way as the vertical navigation buttons - changing styling, including background color, etc. Additionally, there will be external links in the #footer section, so that needs to be considered as well.

Check out the JSFiddle here.

 $(function(){

 // Handling clicks on the vertical navigation on the left side of #content
 $('#sidemenu a').on('click', function(e){
 e.preventDefault();

 if($(this).hasClass('open')) {
  // Do nothing since the link is already open
 } else {
  var oldcontent = $('#sidemenu a.open').attr('href');
  var newcontent = $(this).attr('href');


  $(oldcontent).fadeOut('fast', function(){
    $(newcontent).fadeIn('fast').removeClass('hidden');
    $(oldcontent).addClass('hidden');
  });


  $('#sidemenu a').removeClass('open');
  $(this).addClass('open');
  }
 });

 // Handling clicks on the bottom links
 $('#footer a').on('click', function(e){
 e.preventDefault();
 var oldcontent = $('#sidemenu a.open').attr('href');
 var newcontent = $(this).attr('href');
 // Check if $(this) element links to a tab content or external
 if($(this).hasClass('open-tab')){
     if (oldcontent == newcontent){
        // If this tab is already open, do nothing to page
     } else {
         $('#content ' + String(oldcontent)).fadeOut('fast', function(){
         $(newcontent).fadeIn('fast').removeClass('hidden');
         $('#content ' + String(oldcontent)).addClass('hidden');
         });

         $('#sidemenu a').removeClass('open');
         $('#sidemenu a ' + newcontent).addClass('open');
     }
 } else{ 
   // Just use href link to whatever the element's href attribute is
   window.open($(this).attr('href'));
 }

 });
 });

Answer №1

The issue with the functionality of your links in #footer compared to those in #sidebar stems from a specific line present in both their click functions:

var oldcontent = $('#sidemenu a.open').attr('href');

This line is attempting to locate the current content by inspecting the classes of child elements within #sidebar.

If you select a link at the bottom, the subsequent link you click on (in either #sidebar or #footer) will fail to find the current content because it resides in #footer, not #sidebar. To rectify this, you must devise a method to set var oldcontent based on both #sidebar and #footer components.

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

Attempting to utilize Flexbox to populate vacant areas

https://i.stack.imgur.com/BhPU0.png Can the yellow square smoothly transition to the empty grey square and beyond in this layout setup? Each flex item represents a component that can utilize either 50% or 100% of the container width, information only know ...

Unable to detect hover (etc) events after generating div elements with innerHTML method

After using the code below to generate some divs document.getElementById('container').innerHTML += '<div class="colorBox" id="box'+i+'"></div>'; I am encountering an issue with capturing hover events: $(".colorB ...

Extracting precise information from HTML documents

Looking to extract specific data from an HTML file using C# and HtmlAgilityPack. Here is a snippet of the HTML: <p class="heading"><span>Greeting!</span> <p class='verse'>Hi!<br> // Hello!</p>& ...

Error: The function Object.entries is not defined

Why does this error persist every time I attempt to start my Node.js/Express server? Does this issue relate to the latest ES7 standards? What requirements must be met in order to run an application utilizing these advanced functionalities? ...

What methods and applications are available for utilizing the AbortController feature within Next.js?

My search application provides real-time suggestions as users type in the search box. I utilize 'fetch' to retrieve these suggestions from an API with each character input by the user. However, there is a challenge when users quickly complete the ...

Dynamic web designs can be achieved by utilizing a combination of technologies such as Ajax

Encountering issues while attempting to use Ajax and Fancybox.js photo viewer simultaneously. The website is initially set up as web 1.0 with standard navigation using hyperlinks. For html5 browsers, a JavaScript is utilized to create a web 2.0 experienc ...

The use of JQuery datatables and the impact on data loading speed

, I have incorporated JQuery datatable to showcase all the projects stored in my database: <script type="text/javascript" charset="utf-8"> $(document).ready(function(){ $('table.display').dataTable({ "bJQueryUI": tr ...

Strategies for Preserving Added Field Data Using JQuery

I am working on a code that dynamically adds fields to a form, you can see it in action on this jsFiddle. My question is, is there a way to keep the newly added fields even after the form is submitted and the user returns to the page? I'm not looking ...

Facing an issue with displaying a component error in a mat-form-field in Angular 9

I am looking to develop a shared component for displaying errors in Angular Material. Here is my shared component pfa-share-error: <mat-error *ngIf="fieldErrors(fieldName).required && fieldErrors(fieldName)"> Required </mat-err ...

What is the best way to fetch and convert information from an API to display on a website?

I am encountering an issue while trying to retrieve data from an API. Below is my code with a fabricated access code. $(function () { var $data = ('#data'); $.ajax({ type: 'GET', url: 'http://api.openweathe ...

Is there any more Angular code to be bound using ng-bind-html or ng-bind?

Hey there! Just a quick Angular inquiry: <div class="_minimal_size margin_10_middle"> <div class="_50 espaciado_0_20"> <p ng-bind-html="eirana_knows.feedback"></p> </div> <br class="clear"/> </div ...

What is the best way to retrieve dates from a MySQL database using ExpressJS?

My current task involves retrieving the date value from a MySQL server, but upon printing the result, I encounter an error. In my code, I am able to fetch the date value, however, when attempting to print it, there seems to be an issue with the output. (F ...

How to effectively manage draggable items or remove draggable data properly

I am looking to make certain objects draggable, however, the set of objects varies every time. How can I clear the draggable data from the previous set? P.S. When I use $('.draggable').draggable('destroy');, it gives me an error saying ...

Let us know when the information on a different tab is updated

I have multiple tabs and I want to indicate when a change occurs on another tab that the user hasn't clicked. For example, if the user hits the run button while on the Data pane, I would like the Errors tab to change to red to show that there was a ch ...

Every time I use Get for ajax calls, I keep getting hit with a frustrating

Initially, I was making a call to a [web method] using the POST method. However, since I need to receive data back, I attempted to switch to using the GET method instead. The previous implementation using POST was successful. Unfortunately, using GET resu ...

Switching class names on click in Vue.js

When clicked, I would like to toggle the class name from "product fav" to "product fav active". The change should be conditional; if the class is already active, it should be removed. ...

Dealing with ajax requests when there is no network connection and the wifi is turned off on an Android

Currently, I am in the process of creating an Android Application with phonegap. While everything is functioning correctly with the request/response handling, I am encountering a challenge when there is no 3G network available and WiFi is disabled. In this ...

Adjust the size and position of the text input field

I'm struggling to find a way to both resize and move the box without one action interfering with the other. Whenever I attempt to change the size of the box, it triggers the move event at the same time. Clicking on the box should make it resizable, bu ...

Attempting to hash the password led to encountering an error

An issue was encountered: both data and salt arguments are required. This error occurred at line 137 in the bcrypt.js file within the node_modules directory. The code snippet below highlights where the problem is present: const router = require("express" ...

The error message "email() is not a valid function when using the onclick attribute

Can anyone lend a hand? I feel like I must be overlooking something really obvious. I'm having trouble calling my function to execute my ajax call. Any assistance would be greatly appreciated. Thank you! Here is an excerpt of the HTML code: $(docu ...