S
S
Sergey2018-03-04 17:39:11
JavaScript
Sergey, 2018-03-04 17:39:11

How to implement select and exclude in jquery?

There are two lists (ul>li*10). It is necessary that when clicking on li, an action is applied to all li except the current one.

I sort of did this (how correctly I'm not sure, you can certainly do better)

$('ul.left li, ul.right li').on('click', function () {
    var sel = $('ul.left li, ul.right li');
    sel.css('opacity','1');
    sel.not(this).css('opacity','.2');
    sel.addClass('active');
    sel.not(this).removeClass('active');
  });


And now how can I make all styles return when clicking on li where there is an active class?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Andrew, 2018-03-04
@KickeRockK

$('ul.left li, ul.right li').on('click', function () {
    var sel = $('ul.left li, ul.right li');
    sel.hide();
    $(this).show();
  });

If you don’t just need to hide, then you do what is needed for everyone and then highlight a specific one. this is here just to understand which one you clicked on (from the entire li's array)
For example, instead of .hide() and .show() give them the same active class and set styles for it, and there will be less code in css just styles (that's what it's for)
$('ul.left li, ul.right li').on('click', function () {
    var sel = $('ul.left li, ul.right li');
    sel.removeClass('active');
    $(this).addClass('active');
  });

.active {
opacity: 1;
}
ul.left li, ul.right li {
opacity: 0.2;
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question