-1

I want to select all elements on a page and add a class to them. But two of them shouldn't get this class.

$(document).on('click', '[data-show-link]', function() {
    var $this = $(this);
    $('[data-show-id]').toggle();
    $('*').not($this).not('[data-show-id]').toggleClass('get-opacity'); // ?
    event.preventDefault();
});

http://jsfiddle.net/ynts/8hLQG/

A. Z.
  • 728
  • 1
  • 11
  • 28
  • what's the question / problem? – Alnitak May 29 '14 at 10:22
  • `$("*")` matches *all* elements, including containers (e.g. the `` element). Opacity cascades down to descendant elements whether or not they expose the `get-opacity` class. You have to refine your first selector. – Frédéric Hamidi May 29 '14 at 10:23
  • very dangerous to use `$('*')` this way, its performance is also bad. So narrow down the context by `$('.wrap *')` instead. – King King May 29 '14 at 10:24
  • By far **not** the best solution: `$('*').not('body,html').not($(this)).not('[data-show-id]').toggleClass('get-opacity');` but a *quick and dirty* one. – http://jsfiddle.net/8hLQG/3/ – Robin May 29 '14 at 10:25
  • http://stackoverflow.com/questions/2891452/jquery-data-selector , go through this tutorial ... –  May 29 '14 at 10:28
  • @ynt you can update as suggested in the answer : http://stackoverflow.com/a/23931163/1059101 – Jai May 29 '14 at 11:11

2 Answers2

0

change this:

$('*')

to this:

$('.wrap').children()

Demo

Jai
  • 74,255
  • 12
  • 74
  • 103
0
$("p").not("[data-show-id]").toggleClass('get-opacity');

JSFIDDLE : Isn't this you're looking for ?

Mahbub
  • 3,108
  • 24
  • 29
  • You are right, your solution works with my JSfiddle example. However, that's not the complete solution I'm looking for. My example is a just simplified version of a whole page. In fact, I want to add class to all page elements, not only `p`. – A. Z. May 29 '14 at 10:57