I have a rather strange scenario (web scraper). I want to select class=g , but not those with class="gg" (double class g ). How to do it in jQuery?
class=g
class="gg"
g
If I use $ ('. G'), it will select both .g and .g .g
.g
.g .g
UPDATE 1:
If you don't think .g .g valid, check out the source in Google search results;)
Given the following scenario, in which you have two div tags with the above classes
<div class="g"></div> <div class="gg"></div>
Write the following to get only those divs that have class "g"
alert($("div[class='g']").length);
Even if I don't think gg really (let me check) .. and therefore $(".g:not(.gg)") does not work, you can do something like:
gg
$(".g:not(.gg)")
var myWeirdElements = $('.g').map(function(){return this.className.indexOf('g g') && this ;}); // or, as mentioned on the comment, using Array.filter method
demo => http://jsfiddle.net/GeAaA/
Edit: about the second comment, you just need to calculate how many g (simple regular expression)