Facing a few hiccups in JavaScript or CSS code, but getting closer to making it work

Here is the code I currently have with HTML, CSS, and JavaScript. Everything is working correctly except for one small issue that I can't seem to resolve. I am new to JavaScript and believe that JavaScript is necessary to fix this problem. If not, then it might be a CSS issue...

If you click on each div with titles such as "NoS Members," "Registered Members," and "Team Profiles," you will see that the slider background image (small triangle) works and becomes active when clicking any of the three divs. This action reveals another container below, which functions perfectly. However, the problem I encounter is that if you initially click on "NoS Members," then proceed in order to click either the 2nd one - "Registered Members," or the last one - "Team Profiles," and then try going back to click on "Registered Members" or "NoS Members" again, the slider does not work by sliding back to the left. It only seems to work sliding to the right after an initial click has been made.

Check out my jsfiddle here: http://jsfiddle.net/5DTKH/

Code:

HTML

<div id="profile_selection">
    <a href="#nos_profiles" class="profile_selection">{Ñا}<br />Members</a>
    <a href="#registered_profiles" class="profile_selection">Registered<br />Members</a>
    <a href="#team_profiles" class="profile_selection">Team<br />Profiles</a>
    <div id="profile_selection_slider"></div>
</div>
<div id="nos_profiles" class="selection">
</div>
<div id="registered_profiles" class="selection">
</div>
<div id="team_profiles" class="selection">
</div>

CSS

#profile_selection { width: 612px; height: 152px; padding: 0; margin: 15px auto; position: relative; }
#profile_selection a {
  width: 200px;
  height: 105px;
  padding: 45px 0 0 0;
  margin: 0;
  background: #333;
  border: 2px solid #444;
    -moz-border-radius: 25px;
    -webkit-border-radius: 25px;
    border-radius: 25px;
    -moz-box-shadow: inset 0 -0.3em 0.9em 0.3em #000, 0 28px 24px -24px #000;
    -webkit-box-shadow: inset 0 -0.3em 0.9em 0.3em #000;
    box-shadow: inset 0 -0.3em 0.9em 0.3em #000;
  float: left;
    -moz-transition: all .2s ease;
    -webkit-transition: all .2s ease;
    -o-transition: all .2s ease;
    transition: all .2s ease;
  color: #FFF;
  font: 24px Arial, Helvetica, sans-serif;
  font-weight: bold;
  font-variant: small-caps;
  text-align: center;
  text-decoration: none;
  text-shadow: 1px 1px 1px #000, -2px -2px 2px #000;
  position: relative;
  z-index: 4;
}
(...)

Javascript

$(document).ready(function(){
    $('a.profile_selection').click( function(){
       var a = $(this);
       $('a.profile_selection').removeClass('active');
       $(this).addClass('active');
       var selection = $( a.attr('href'));
       selection.removeClass('selection');
       $('.selection').hide();
       selection.addClass('selection');
       if( selection.is(':visible')){
           selection.slideToggle(400)
       }else{selection.slideToggle(400)
        };
    });
});

I would greatly appreciate any assistance. Also, just mentioning that I am using the jQuery library 1.3.2 - I understand it's outdated, but that's what I have... Thanks to Ashis and Nick for their previous help as well.

Answer №1

I have recently updated the code above and made some modifications. You can view the updated fiddle by clicking on this link: Check out the Fiddle Here

 $(document).ready(function(){
var clicked;
$('a.profile_selection').click( function(){
   var a = $(this);
    clicked=$(this);
 $('a.profile_selection').not(clicked).removeClass('active');
   $(this).addClass('active');
   var selection = $( a.attr('href'));
   selection.removeClass('selection');
   $('.selection').hide();
   selection.addClass('selection');
   if( selection.is(':visible')){
       selection.slideToggle(400)
   }else{selection.slideToggle(400)
    };
});
$('a.profile_selection').hover(function(){
var a = $(this);
   $('a.profile_selection').not(clicked).removeClass('active');
   $(this).addClass('active');
  })
});

Answer №2

make the following changes to your css lines 53 to 55:

#profile_selection a:nth-of-type(1).active ~ #profile_selection_slider{ left: 71px; }
#profile_selection a:nth-of-type(2).active ~ #profile_selection_slider { left: 275px; }
#profile_selection a:nth-of-type(3).active ~ #profile_selection_slider { left: 480px; }

#profile_selection a:nth-of-type(1):hover ~ #profile_selection_slider{ left: 71px; }
#profile_selection a:nth-of-type(2):hover ~ #profile_selection_slider{ left: 275px; }
#profile_selection a:nth-of-type(3):hover ~ #profile_selection_slider{ left: 480px; }   

this adjustment allows the :hover effect to work even when other #profile_selection items are active. Click here for demonstration.

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

What could be causing the tooltip to malfunction in jQuery?

The tooltip is not appearing as expected, only the title is displaying in the browser. What can be done to fix this? Below is the code snippet: HTML Code: <form action="/validate.php" method="post" id="myform"> <table width="100%" border="1"> ...

Retrieve the output of a Node.js function

I have a function that extracts data from a website and displays it in the console. However, I now want to return this data so I can perform additional actions with it. Instead of using console.log(temperature), I would like to retrieve the temperature v ...

Are leap seconds taken into account in macOS time updates?

I wonder if leap seconds have any impact on the time displayed on my computer. Upon inspecting the JavaScript date object, it seems that leap seconds are not accounted for at all. They do not seem to be included in the base representation. It appears tha ...

Disabling a SELECT OPTION in AG-Grid: A step-by-step guide

Is there a way to disable a dropdown element in AG-Grid using React? I've seen examples like the one on W3Schools where you can disable an option in a dropdown, but I'm not sure how to implement this in AG-Grid. The dropdown I'm working wit ...

Tips for updating data-ng-init with a directive

Is there a way to automatically refresh the variable assigned in data-ng-init whenever the source data changes, without having to do it through the controller? I'm looking for a directive that can achieve this. Any suggestions or examples would be ap ...

Creating services in Angular is a continuous process

How can I create a service as singleton in Angular? I have a service that is injected into 2 components and the value is set to true. However, every time I open the view, the service is created again and the value resets to false. How can I make sure the ...

What are the limitations of using a CSS transition alongside a separate animation created with jQuery's animate method?

I am facing an issue with moving and flipping a button simultaneously when clicked. I have set up two animations to happen at the same time, each with the same duration. However, only the CSS scale transform seems to be working while using both jQuery&apos ...

How to vertically center content using Bootstrap 4 and Laravel

Seeking to achieve vertical centering of my column content. <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css"> <div class="container d-flex h-100"> <div class="row mt-5 "> < ...

Utilizing Laravel 8 for seamless file uploads with AJAX on onchange event

I'm trying to implement a feature in my Laravel 8 application where users can upload multiple files using the onchange event. Is it possible to achieve this functionality with just the onchange event? Below is the HTML form I currently have. Any assis ...

retrieving MySQL data within an AngularJS webpage

I am completely new to Angular and I really need to figure this out quickly. My goal is to pull information from a mysql table into my Angular page, but I seem to be stuck. I'm not sure where I'm going wrong or what needs fixing. Here is the HTML ...

The JavaScript onunload event handler

Despite all my research, I'm still unable to solve this issue. My task involves executing some functions when a user leaves my page. To achieve this, I utilized window.onbeforeunload to warn the user about leaving the page and window.onunload to initi ...

Is there a way to verify the 410 status of an iframe?

I am currently developing a webpage that utilizes the Slideshare API to fetch a select number of presentations from a specific Slideshare user and display them through embedded iframes. Everything is functioning correctly for the most part, except for one ...

Using Special Characters in React JS Applications

When handling CSV uploads with accented characters such as émily or ástha, I encountered the need to encode and pass them to the backend. Experimenting with different approaches, I tried adjusting the file type in FormData from 'text/plain' to ...

Strange occurrences with Javascript events, removeEventListener failing to function

I am facing a challenge with removing event listeners in my code to prevent multiple calls to an event. I need the flexibility to call these functions multiple times without causing conflicts. Currently, my solution is not effectively removing the event l ...

Firefox browser does not display flashing titles for tabs

Every second, I want to display "New message..." in the title when the browser tab is inactive or the user is in another tab. Here's the code I used: <script> var mytimer; function log() { document.title = document.title == "" ...

The GIPHY API object returns no results

Utilizing Angular 2 to fetch data from the GIPHY API. export class ListaGifsComponent { gifs : Object[] = []; urlBase = "http://api.giphy.com/v1/gifs/search?q="; termoPesquisado = "ryan+gosling"; key = "O8RhkTXfiSPmSCHosPAnhO70pdnHUiWn"; ...

The reset button in Formik is not working to clear the form as intended

I currently have the following setup: <Formik initialValues={{}} onSubmit={data => console.log(data)}> <Form className="h-full"> <FieldsWrapper className="hide-scrolling-bar"> ...

Interactive jQuery slider for iPhone that shifts background image while maintaining stationary red bar at center

Looking for a way to make this image into an interactive slider for iPhone users. The goal is for users to swipe their finger across the screen to change the channel. Is it possible to achieve this functionality with a slider? I understand how a slider kn ...

React Router version 6.4.5, automatic redirection upon loading the page

Seeking assistance with setting up redirects. Below are snippets of the code. index.js const router = createBrowserRouter([ { //Defining App as the root element... path: "/", loader: () => { }, element: <App/>, ...

Troubleshooting Issue with Bootstrap 4 and Rails 6: Unusual Error Message in ".js.erb" File - Issue with $(...).modal Functionality (Works in Other .js Files)

I'm currently working on integrating Bootstrap modals into a Rails 6 project. The modal comprises a form that utilizes Ajax for submission. My objective is to automatically hide the modal upon successful form submission. However, if there are errors d ...