0

I want a part of my page not to be parsed by the jQuery Selector.

In the example below, is there a way to get all the ".module" elements but exclude the one under the div which has the class "exclude" ?

Moreover, can we do with Jquery something like "please do not parse this area". For example, I have a page which has a div(#huge) with a HUGE html content, is there a way for Jquery to ignore this div(#huge) ?

For example, I have this structure:

<div id="page">
  <div class="exclude">
    <div class="module"></div>
    <!-- HERE HUGE HTML -->
  </div>
  <div>
    <div class="module"></div>
    <div class="module"></div>
  </div>
</div>
BoltClock
  • 700,868
  • 160
  • 1,392
  • 1,356
dervlap
  • 406
  • 4
  • 14

3 Answers3

3
$modules = $('div:not(".exclude") > .module');

http://api.jquery.com/not-selector

Blazemonger
  • 90,923
  • 26
  • 142
  • 180
  • 2
    Drop the quotes inside the `:not()`, they're not needed (even though they're allowed [for some weird reason](http://stackoverflow.com/questions/12475595/why-do-the-not-and-has-selectors-allow-quoted-arguments)) and leaving them out can improve performance. – BoltClock Nov 15 '12 at 18:06
1

In your precise case, the key is to look only in the right divs.

You would start by making the searchable collection

var $good = $('#page>div:not(.exclude)');

And all your queries would be built as $(yourquery, $good) :

var $mymodules = $('.module', $good);

Demonstration (open the console)

Note that when looking for an element by its id, it still would be more efficient to use $('#theid') rather than $('#theid', $good).

Denys Séguret
  • 372,613
  • 87
  • 782
  • 758
0
$modulesNotTheHugeOne = $(".module").not("#huge");

selects all with class module but not the one with id huge

http://api.jquery.com/not/

st3inn
  • 1,556
  • 9
  • 17