Select the element to emphasize it and reduce the visibility of others

I'm currently facing an issue with a block that contains multiple divs representing products. My goal is to have the selected product highlighted while the rest appear less prominent when clicked. I've managed to achieve this functionality, however, every time a new product is clicked, all previously selected products also become less sharp.

Below is my current jQuery code:

$(document).ready(function () {

  var $all_listItems = $('.choose-format-block .format-area a');
  $all_listItems.on('click', function () {
    var index = $all_listItems.index(this);
    $(this).siblings().stop().fadeTo(300, 0.6);
    $(this).parent().siblings().stop().fadeTo(300, 0.3);
  });

});

Here's a link to my working example on jsfiddle: http://jsfiddle.net/justamir/P7b5T/2/

If anyone could provide assistance with this (I am new to jQuery), it would be greatly appreciated.

Answer №1

To achieve this effect, I recommend using CSS opacity and transitions instead of jQuery animations. You can view the updated fiddle here:

http://jsfiddle.net/P7b5T/3/

$(document).ready(function () {
    var $all_listItems = $('.choose-format-block .format-area a');
    $all_listItems.on('click', function () {
        $('.container').addClass('selected');
        $('.format-area.active').removeClass('active');
        $(this).parents('.format-area').addClass('active');
    });
});

CSS:

.container .format-area {
    opacity: 1;
    -webkit-transition: opacity .3s;
    transition: opacity .3s;
}

.container.selected .format-area {
    opacity: .3;
}

.container.selected .format-area.active {
    opacity: 1;
}

The "selected" class is added to the container when an item is selected, changing all items' opacity to .3. By adding the "active" class to the selected item, its opacity returns to 1.

EDIT: To deselect all items, simply remove the "selected" class from the container.

Answer №2

The issue lies in not properly resetting the fadeout div that is already active.

To resolve this, consider implementing the following approach: Upon every click event, initiate a reset of all items before proceeding to adjust the opacity levels using the fadeTo method for all other elements except the one that was clicked.

$(document).ready(function () {

// Click behavior for product size selection
    var $all_listItems = $('.choose-format-block .format-area a');
    $all_listItems.on('click', function () {
        $(this).parent().parent().children().stop().fadeTo(300, 1); // Reset all items
        $(this).parent().siblings().stop().fadeTo(300, 0.3); // Fade out others
    });

});

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

Tips for streamlining the use of hidden and visible div elements with concise code

I have been attempting to use the same code for multiple modules on this page, but none of the methods I've tried seem to be effective. I want to avoid endlessly duplicating existing code. document.addEventListener('DOMContentLoaded', fun ...

Is the CSS3 bar chart flipped?

I'm currently developing a responsive bar chart, and everything seems to be going smoothly except for one issue - when viewing it in full screen, the bars appear to be flipped upside down. It's quite puzzling as all other elements such as text an ...

Text that is curving around a D3.js pie chart

I am currently working on creating a 3D-Js chart and I would like the pie text to wrap around the pie itself. This is the exact effect that I am trying to achieve: https://i.sstatic.net/YOLdo.png I am facing two main issues: I am currently printi ...

Achieve the effect of making the Bootstrap JS Collapse text bold after it has been

Is there a way to make Bootstrap JS Collapse text Bold after it has been clicked on? <tr data-toggle="collapse" data-target="#demo8" class="accordion-toggle"> <td> <div class="fa ...

Which is more effective: utilizing the "hidden" CSS attribute or fetching every new set of images individually?

Currently, I am attempting to retrieve a collection of photo data from Flickr by making a jQuery AJAX request using JSONP. The goal is to exhibit n images in succession, then proceed to display the next set of n images whenever the user selects a button. ...

Optimal practices for naming divs in XHTML/CSS

Is it acceptable to use spaces in div names like this? <div class="frame header">Some content here</div> Having a space in the name might require separate CSS classes such as: .frame { display: block; } .header { background-color: #efefef; } ...

How can I reset a CSS position sticky element using JavaScript?

I have created a page where each section fills the entire screen and is styled using CSS position: sticky; to create a cool layered effect. Check it out here: https://codesandbox.io/s/ecstatic-khayyam-cgql1?fontsize=14&hidenavigation=1&theme=dark ...

Would it be possible to use a script to convert the y-axis values of the chart into Arabic numerals?

Currently, I am working on creating line and pie charts, but I need the Y-axis to display Arabic language. $('button').on('click', function(e) { $('#pie-chart3').empty(); var type = $(this).d ...

The backend error message isn't triggering an alert!

My current issue involves using jQuery to execute a .php file. Whenever an error occurs in the backend, I want to display an alert message with the error details. However, when intentionally submitting with an error, no alert pops up and it just proceeds t ...

Leveraging tailwind for achieving dynamic height responsiveness

My Tailwind-built page currently has a table in the bottom left corner that is overflowing, and I would like it to adjust its size to fit the available space and scroll from there. I want it to fit snugly in the bottom left corner of the browser viewport ...

jQuery: Retrieve data from a table

I am currently working on using an HTML table as a look-up table. Normally, I would handle this server-side, but in this particular case, the client will be inputting the table. When filling out a form, they provide two values from columns A and B, which t ...

Make the text area occupy the full width of the remaining screen space

I've been struggling to make the text area fill the remaining width of the screen. Everything is set up nicely, but I just can't seem to find a solution to get it to expand and occupy the rest of the available space. I intentionally refrained fr ...

Div sliding out of view

I'm encountering a slight issue with this template: essentially, my goal is to implement a feature where clicking on a box will expand it while simultaneously sliding the other boxes off-screen. However, instead of just sliding the div off-screen, it ...

Tips on working with an array received from a PHP script through AJAX

I've been stuck with this issue for the past few hours and I'm hoping to find a solution here. What I'm attempting to do is something like the following: PHP: $errorIds = array(); if(error happens){ array_push($errorIds, $user['user ...

What's the best way to save data from an HTML input field into a JavaScript array?

Can someone help me with storing a value from an HTML input field into a JavaScript array after a button click, and then displaying that array data on the screen? Here is the HTML code I am currently working with: <!DOCTYPE HTML> <html> < ...

Retrieve data from the database at the optimal time interval, and halt the process once the data has been successfully received

I'm new to this, so please bear with me :) What is the easiest way to check for, fetch, and utilize newly received data from a MySQL database? The database is constantly updated through an external API. Ideally, I would like to capture the data as i ...

Interactive horizontal web design with center alignment and dynamic scrolling feature

We have designed a unique web layout that is centered horizontally and includes an animated scrolling effect. There are 5 menu options that use simple anchor links to navigate between different pages. Our layout features static content with only the body s ...

Creating a div with a scrollbar limited to a maximum of 100% of the page size

<!-- ********************************************************************************************************************************************************** RIGHT SIDEBAR CONTENT *********************************************************** ...

Is it possible to send form data using the file upload function?

I've successfully implemented a file upload feature with a progress bar, and the file gets uploaded in fileUpload.php. function fileSelected() { var file = document.getElementById('fileToUpload').files[0]; if (file) { var fil ...

Unable to deactivate button using JavaScript following an AJAX request

Within my JS file, I am attempting to disable a button after making an AJAX call: $('document').ready(function() { const runField = $('input[id=run]') const runButton = $('.btn-run') const saveButton = $('.btn-save ...