How to use jQuery to select ids not in a specific class
I have a jQuery script that selects all IDs with "phone" in them. However, I have a small part that should NOT select them if they are in a class.
What I have, as I understand it, is this:
$("[id*='Phone']:not('referencePhones')").doSomething();
What am I missing?
.referencePhones is the parent class. i.e:
div class="referencePhones"
span id="Phone"
+2
Mikecancook
source
to share
4 answers
$("[id*='Phone']").parent(":not('.referencePhones')").css("color","red");
and your code looks something like this:
<div class="referencePhones">
<span id="Phone2">phone2</span>
<span id="Phone3">phone3</span>
<span id="Phone4">phone4</span>
<span id="Phone5">phone5</span>
</div>
<div class="zzz">
<span id="Phone6">phone6</span>
<span id="Phone7">phone7</span>
<span id="Phone8">phone8</span>
<span id="Phone9">phone9</span>
</div>
+3
easement
source
to share
get rid of your internal quotes:
$('someElement[id*=Phone]:not(.referencePhones)').doSomething();
EDIT
$('span[id*=Phone]').parent('div:not(.referencePhones)').doSomething();
+9
Jason
source
to share
Point?
$("[id*='Phone']:not(.referencePhones)").doSomething();
^
+4
slikts
source
to share
OP is asking to select spans with IDs containing "phone" if they are not contained in a div with class "referencePhones". All solutions that use parent () return the parent div, not the span.
The following selector returns spans:
$('div:not(.referencePhones) span[id*="Phone"]')
0
keithm
source
to share