What is the best way to ensure only one item is being selected at a time?

In the initial state, I want 'ALL' to be automatically selected. Clicking on another item should change the classes so that only 'this' has the class and the others do not. However, I am facing an issue where 'ALL' cannot be re-selected.

Check out the jsFiddle example here: http://jsfiddle.net/u5g9vLkx/

HTML:

<ul class="nav2">
    <li><a href="#" class="orange2">ALL</a></li>
    <li><a href="#" class="orange">PERSONAL</a></li>
    <li><a href="#" class="orange">PHOTOGRAPHY</a></li>
    <li><a href="#" class="orange">WORK</a></li>
</ul>

CSS:

body{
    background: #000000;
}
.nav2{
    float: none;
    list-style-type:none;
    overflow: hidden;
    clear: both;
    text-align: left;
    display: inline-flex;
}

.nav2 li{
    clear: both;
    overflow: hidden;
    margin-left: 10px;  
}

.orange{
    opacity: .5;
    color: #FF9000;
    text-decoration: none;
    display: block;
    text-align: center;
    padding: 8px;
    border: 1px solid #000; 
    -webkit-transition: all 0.2s ease-out;
    -moz-transition: all 0.2s ease-out;
    -ms-transition: all 0.2s ease-out;
    -o-transition: all 0.2s ease-out;
    transition: all 0.2s ease-out;
}

.orange:hover{
    opacity: 1;
    color:#000000;
    background: #FF9000;
}

.orange2{
    color: #FF9000;
    text-decoration: none;
    display: block;
    text-align: center;
    padding: 8px;
    border: 1px solid #FF9000; 
    -webkit-transition: all 0.2s ease-out;
    -moz-transition: all 0.2s ease-out;
    -ms-transition: all 0.2s ease-out;
    -o-transition: all 0.2s ease-out;
    transition: all 0.2s ease-out;
}

.orange2:hover{
    color:#000000;
    background: #FF9000;
}

Javascript:

$('a.orange').click(function(){
$('a.orange2').addClass('orange');
$('a.orange').removeClass('orange2');
$(this).removeClass('orange');
$(this).addClass('orange2');
});

Answer №1

When writing your JavaScript code, make sure to apply the onClick listener to both a.orange and a.orange2 (a elements with the classes orange and orange2).

$('a.orange, a.orange2').click(function(){ ... });

If you only apply it to a.orange, elements with the class orange2 like the "ALL" menu entry will not be affected.

By including a.orange2 in the selector, you can ensure that both classes trigger the onClick event:

$('a.orange, a.orange2').click(function(){ ... });

Answer №2

Perhaps consider this suggestion:

 <ul class="nav2">
      <li><a href="#" class="orange2">ALL</a></li>
      <li><a href="#" class="orange2">PERSONAL</a></li>
      <li><a href="#" class="orange2">PHOTOGRAPHY</a></li>
      <li><a href="#" class="orange2">WORK</a></li>
 </ul>

 $('a.orange2').click(function(){
      $('a.orange2').removeClass('orange2').addClass('orange');
      $(this).addClass('orange2').removeClass('orange');
 });

http://jsfiddle.net/khzehpfx/

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 API response into a class instance using `class-transformer` in TypeScript: A step-by-step guide

When working with TypeScript, I have a regular method called Request(method: HttpMethod, url: string, ...) that is used for calling APIs. Now, my goal is to convert the response from this API request into an instance of a class using class-transformer (or ...

How can I handle JSON data that contains undefined values?

Similar Question: Is it possible to parse JSON with special 'undefined' values using JavaScript? I'm curious if there's a way to parse something like javascript JSON.parse('{ "name": undefined}'); that is generated by an API ...

Having trouble retrieving response content in Mithril

I've been experimenting with making a request to a NodeJS API using the Mithril framework in my client application. I attempted to fetch data by following their example code: var Model = { getAll: function() { return m.request({method: "G ...

Encountering an issue with double quotes formatting in Spring Boot applications

Currently, I am developing an application where I need to pass data from a Spring Boot controller to a template using Thymeleaf. Everything was going smoothly, until I encountered an issue when trying to send JSON data. I noticed that the double quote (" ...

Contrasting the use of jQuery versus AJAX for updating static page text

While I haven't fully grasped the concept of AJAX yet, my understanding is that it can be beneficial for updating specific parts of a webpage. Does using AJAX only make sense when you require server interaction? I am looking to replace text on a webp ...

Blank YouTube Popup Modal

I am in the process of creating a pop-up modal that contains an embedded YouTube video, but my modal is not displaying properly. I am utilizing Bootstrap and have two separate sections along with JavaScript code. When the modal appears, it remains empty. I ...

How can you make .ajax and params appear in this format?

I attempted to post a question earlier, but it seems like it was too complicated. Essentially, the request was for data to be formatted in a specific way, not from a form submission but generated through client-side search results. The desired format to ...

Attempting to showcase a pair of unordered lists side by side using the power of flexbox

I am having trouble getting this to display on a single line, no matter what I try. While there may be simpler ways to achieve this without using flexbox, I am eager to learn how to do it using flexbox properties. My desired outcome is to have the first u ...

Obtaining information from a intricate string input

{JSON.stringify(walletData.transactions, null, 2)} My goal is to extract backend data and display it as a table. The response has been converted into an array as shown below. [ { "date": "Nov 07, 2023", "description" ...

What is the best way to center a Checkbox in HTML5 and CSS?

I've been struggling to center my Checkbox with label on my current website project. Despite searching online for solutions, I'm still unable to find the right method! If you're curious, you can check out the source code of my webpage at ( ...

Are there any other CSS methods available to address the limited support for flex-grow in Safari?

I'm currently developing an interactive "accordion" component that can accommodate multiple child elements. Each child item closes to the height of its header, but expands evenly with other open siblings when activated. If this explanation is unclear, ...

While attempting an AJAX request with jQuery, I encountered the following error message: "Error: ER_SP_UNDECLARED_VAR: Undeclared variable: NaN"

I encountered an issue that says: ` throw err; // Rethrow non-MySQL errors ^ Error: ER_SP_UNDECLARED_VAR: Undeclared variable: NaN ` while attempting a jQuery AJAX get request, and I'm unsure of the cause. My backend is built using node.js a ...

Customize Angular Material styles uniquely across various components

Within my application, I am utilizing two components that contain tab groups. The first component serves as the main page, where I have adjusted the CSS to enlarge the labels by using ViewEncapsulation.None. The second component is a dialog, and I aim to m ...

FFmpeg: audio synchronization issue observed with simultaneous usage of xfade and acrossfade techniques

Experiencing an issue when attempting to concatenate 12 videos together and maintain the audio using xfade and acrossfade filters. Without the audio stream, the video encodes correctly, but when combining with audio, transitions hang and audio sync is off. ...

I incorporated a YouTube video using the iframe tag, but encountered an issue where the page would not scroll up or down when the mouse cursor was positioned on the iframe

How would you like your HTML code to function? When the cursor is on the iframe, do you want it to play a video? Also, should the parent page scroll when you scroll? <section class="section section-padding"> <div class="container"> ...

NodeJS: Error - Unexpected field encountered in Multer

Having trouble saving an image in a SQL database and getting the error message MulterError: Unexpected field. It worked fine in similar cases before, so I'm not sure what's wrong. H E L P!! <label id="divisionIdLabel" for="divis ...

Rendering an array of objects in Three JS

I am encountering an error while trying to render an array of objects in a three.js scene. The error message from three.js reads as follows: three.module.js:8589 Uncaught TypeError: Cannot read property 'center' of undefined at Sphere.copy (thr ...

The error function is consistently triggered when making an Ajax POST request, even though using cURL to access the same

I have been using Ajax's POST method to retrieve a JSON response from the server. However, whenever I click the button on my HTML page, the Ajax function always triggers the error function, displaying an alert with the message "error." Below is the co ...

Exploring the vueJS canvas life cycle and transitioning between components

Struggling with displaying Vue components that contain a canvas with different behaviors. Looking to switch components using v-if and passing different properties. Here's a small example: Currently, when switching from 'blue' to 'red ...

Issue with displaying value after rendering

After fetching pool data from the constants file, my task was to create a featured vault - the one with the highest APY Reward value obtained from the API. Even though I successfully fetched all three values and performed the calculations, I am facing an i ...